Skip to content

Commit

Permalink
Implement a workaround for panic in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Dec 12, 2019
1 parent 72d06e6 commit da3569e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
30 changes: 21 additions & 9 deletions pkg/router/route_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,21 +465,33 @@ func pushPackets(ctx context.Context, t *testing.T, from *transport.Manager, to
panic("malformed packet")
}

to.readChMu.Lock()
select {
case <-ctx.Done():
to.readChMu.Unlock()
if safeSend(to, ctx, payload) {
return
case <-to.done:
to.readChMu.Unlock()
return
case to.readCh <- payload:
to.readChMu.Unlock()
}
}
}
}

func safeSend(to *RouteGroup, ctx context.Context, payload []byte) (interrupt bool) {
defer func() {
if r := recover(); r != nil {
interrupt = r != "send on closed channel" // TODO: come up with idea how to handle this case
}
}()

to.readChMu.Lock()
defer to.readChMu.Unlock()

select {
case <-ctx.Done():
return true
case <-to.done:
return true
case to.readCh <- payload:
return false
}
}

func createRouteGroup() *RouteGroup {
rt := routing.NewTable(routing.DefaultConfig())

Expand Down
8 changes: 2 additions & 6 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ func (r *router) handleDataPacket(ctx context.Context, packet routing.Packet) er
return err
}

if rule.Type() == routing.RuleIntermediaryForward {
switch rule.Type() {
case routing.RuleForward, routing.RuleIntermediaryForward:
r.logger.Infoln("Handling intermediary packet")
return r.forwardPacket(ctx, packet.Payload(), rule)
}
Expand All @@ -352,11 +353,6 @@ func (r *router) handleDataPacket(ctx context.Context, packet routing.Packet) er
r.logger.Infof("Got new remote packet with route ID %d. Using rule: %s", packet.RouteID(), rule)
r.logger.Infof("Packet contents: %s", packet.Payload())

// TODO: maybe move this up along the execution flow? `rg` doesn't seem to be necessary here
if t := rule.Type(); t == routing.RuleForward {
return r.forwardPacket(ctx, packet.Payload(), rule)
}

if rg.isClosed() {
r.logger.Infoln("RG IS CLOSED")
return io.ErrClosedPipe
Expand Down
3 changes: 3 additions & 0 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,13 @@ func TestRouter_handleTransportPacket(t *testing.T) {

fwdRule := routing.ForwardRule(1*time.Hour, dstRtIDs[0], routing.RouteID(7), tp1.Entry.ID, keys[0].PK, keys[1].PK, 0, 0)
cnsmRule := routing.ConsumeRule(1*time.Hour, dstRtIDs[1], keys[1].PK, keys[0].PK, 0, 0)

err = r1.rt.SaveRule(fwdRule)
require.NoError(t, err)

err = r1.rt.SaveRule(cnsmRule)
require.NoError(t, err)

fwdRtDesc := fwdRule.RouteDescriptor()
r1.saveRouteGroupRules(routing.EdgeRules{
Desc: fwdRtDesc.Invert(),
Expand Down

0 comments on commit da3569e

Please sign in to comment.