Skip to content

Commit

Permalink
test: add message exchange test timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
braginini committed Jun 15, 2021
1 parent f247f9a commit 94c0091
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
8 changes: 5 additions & 3 deletions signal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Client struct {
ctx context.Context
stream proto.SignalExchange_ConnectStreamClient
//waiting group to notify once stream is connected
connWg sync.WaitGroup //todo use a channel instead??
connWg *sync.WaitGroup //todo use a channel instead??
}

// Close Closes underlying connections to the Signal Exchange
Expand All @@ -55,11 +55,13 @@ func NewClient(ctx context.Context, addr string, key wgtypes.Key) (*Client, erro
return nil, err
}

var wg sync.WaitGroup
return &Client{
realClient: proto.NewSignalExchangeClient(conn),
ctx: ctx,
signalConn: conn,
key: key,
connWg: &wg,
}, nil
}

Expand Down Expand Up @@ -107,8 +109,8 @@ func (c *Client) connect(key string, msgHandler func(msg *proto.Message) error)
// add key fingerprint to the request header to be identified on the server side
md := metadata.New(map[string]string{proto.HeaderId: key})
ctx := metadata.NewOutgoingContext(c.ctx, md)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
//ctx, cancel := context.WithCancel(ctx)
//defer cancel()

stream, err := c.realClient.ConnectStream(ctx)

Expand Down
1 change: 0 additions & 1 deletion signal/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func (s *SignalExchangeServer) ConnectStream(stream proto.SignalExchange_Connect
}

log.Infof("peer [%s] has successfully connected", p.Id)

for {
msg, err := stream.Recv()
if err == io.EOF {
Expand Down
22 changes: 20 additions & 2 deletions signal/signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var _ = Describe("Client", func() {
Body: &sigProto.Body{Payload: "pong"},
})
if err != nil {
Fail("failed sending a message to {PeerA}")
Fail("failed sending a message to PeerA")
}
msgReceived.Done()
return nil
Expand All @@ -83,7 +83,9 @@ var _ = Describe("Client", func() {
Fail("failed sending a message to PeerB")
}

msgReceived.Wait()
if waitTimeout(&msgReceived, 3*time.Second) {
Fail("test timed out on waiting for peers to exchange messages")
}

Expect(receivedOnA).To(BeEquivalentTo("pong"))
Expect(receivedOnB).To(BeEquivalentTo("ping"))
Expand Down Expand Up @@ -179,3 +181,19 @@ func startSignal() (*grpc.Server, net.Listener) {

return s, lis
}

// waitTimeout waits for the waitgroup for the specified max timeout.
// Returns true if waiting timed out.
func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return false // completed normally
case <-time.After(timeout):
return true // timed out
}
}

0 comments on commit 94c0091

Please sign in to comment.