func (s *TestSuite) TestChannelsAfterReconnect(t *C) { /** * Client send/recv chans should work after disconnect and reconnect. */ ws, err := client.NewWebsocketClient(s.logger, s.api, "agent", nil) t.Assert(err, IsNil) ws.Start() defer ws.Stop() defer ws.Disconnect() ws.Connect() c := <-mock.ClientConnectChan <-ws.ConnectChan() // connect ack // Send cmd and wait for reply to ensure we're fully connected. cmd := &proto.Cmd{ User: "******", Ts: time.Now(), Cmd: "Status", } c.SendChan <- cmd got := test.WaitCmd(ws.RecvChan()) t.Assert(len(got), Equals, 1) reply := cmd.Reply(nil, nil) ws.SendChan() <- reply data := test.WaitData(c.RecvChan) t.Assert(len(data), Equals, 1) // Disconnect client. mock.DisconnectClient(c) <-ws.ConnectChan() // disconnect ack // Reconnect client and send/recv again. ws.Connect() c = <-mock.ClientConnectChan <-ws.ConnectChan() // connect ack c.SendChan <- cmd got = test.WaitCmd(ws.RecvChan()) t.Assert(len(got), Equals, 1) reply = cmd.Reply(nil, nil) ws.SendChan() <- reply data = test.WaitData(c.RecvChan) t.Assert(len(data), Equals, 1) }
func (s *TestSuite) TestChannels(t *C) { /** * Agent uses send/recv channels instead of "direct" interface. */ ws, err := client.NewWebsocketClient(s.logger, s.api, "agent") t.Assert(err, IsNil) // Start send/recv chans, but idle until successful Connect. ws.Start() defer ws.Stop() ws.Connect() c := <-mock.ClientConnectChan <-ws.ConnectChan() // API sends Cmd to client. cmd := &proto.Cmd{ User: "******", Ts: time.Now(), Cmd: "Status", } c.SendChan <- cmd // If client's recvChan is working, it will receive the Cmd. got := test.WaitCmd(ws.RecvChan()) t.Assert(len(got), Equals, 1) t.Assert(got[0], DeepEquals, *cmd) // Client sends Reply in response to Cmd. reply := cmd.Reply(nil, nil) ws.SendChan() <- reply // If client's sendChan is working, we/API will receive the Reply. data := test.WaitData(c.RecvChan) t.Assert(len(data), Equals, 1) // We're dealing with generic data again. m := data[0].(map[string]interface{}) t.Assert(m["Cmd"], Equals, "Status") t.Assert(m["Error"], Equals, "") err = ws.Disconnect() t.Assert(err, IsNil) }