func (s *AgentTestSuite) TearDownTest(t *C) { if s.agentRunning { select { case s.readyChan <- true: // qan.Stop() immediately default: t.Fatal("mock service 1 Stop not ready") } select { case s.readyChan <- true: // mm.Stop immediately default: t.Fatal("mock service 1 Stop not ready") } s.sendChan <- &proto.Cmd{Cmd: "Stop"} // tell agent to stop itself select { case <-s.doneChan: // wait for goroutine agent.Run() in test case <-time.After(5 * time.Second): t.Fatal("Agent didn't respond to Stop cmd") } s.agentRunning = false } test.DrainLogChan(s.logChan) test.DrainSendChan(s.sendChan) test.DrainRecvChan(s.recvChan) test.DrainTraceChan(s.traceChan) test.DrainTraceChan(s.client.TraceChan) test.DrainBoolChan(s.client.ConnectChan()) }
func (s *SenderTestSuite) TestConnectErrors(t *C) { spool := mock.NewSpooler(nil) spool.FilesOut = []string{"slow001.json"} spool.DataOut = map[string][]byte{"slow001.json": []byte("...")} sender := data.NewSender(s.logger, s.client) err := sender.Start(spool, s.tickerChan, 60, false) t.Assert(err, IsNil) // Any connect error will do. s.client.ConnectError = io.EOF defer func() { s.client.ConnectError = nil }() // Tick causes send to connect and send all files. s.tickerChan <- time.Now() t0 := time.Now() // Wait for sender to start trying to connect... if !test.WaitStatus(5, sender, "data-sender", "Connecting") { t.Fatal("Timeout waiting for data-sender status=Connecting") } // ...then wait for it to finsih and return. if !test.WaitStatusPrefix(data.MAX_SEND_ERRORS*data.CONNECT_ERROR_WAIT, sender, "data-sender", "Idle") { t.Fatal("Timeout waiting for data-sender status=Idle") } d := time.Now().Sub(t0).Seconds() // It should wait between reconnects, but not too long. if d < float64((data.MAX_SEND_ERRORS-1)*data.CONNECT_ERROR_WAIT) { t.Error("Waits between reconnects") } if d > float64(data.MAX_SEND_ERRORS*data.CONNECT_ERROR_WAIT) { t.Error("Waited too long between reconnects") } err = sender.Stop() t.Assert(err, IsNil) // Couldn't connect, so it doesn't send or reject anything. t.Check(len(spool.DataOut), Equals, 1) t.Check(len(spool.RejectedFiles), Equals, 0) // It should have called ConnectOnce() serveral times, else it didn't // really try to reconnect. trace := test.DrainTraceChan(s.client.TraceChan) t.Check(trace, DeepEquals, []string{ "ConnectOnce", "ConnectOnce", "ConnectOnce", "DisconnectOnce", }) }
func (s *AgentTestSuite) TearDownTest(t *C) { s.readyChan <- true // qan.Stop() immediately s.readyChan <- true // mm.Stop immediately if !s.alreadyStopped { s.sendChan <- &proto.Cmd{Cmd: "Stop"} // tell agent to stop itself select { case <-s.doneChan: // wait for goroutine agent.Run() in test case <-time.After(5 * time.Second): golog.Panic("Agent didn't respond to Stop cmd") } } test.DrainLogChan(s.logChan) test.DrainSendChan(s.sendChan) test.DrainRecvChan(s.recvChan) test.DrainTraceChan(s.traceChan) test.DrainTraceChan(s.client.TraceChan) test.DrainBoolChan(s.client.ConnectChan()) }
func (s *SenderTestSuite) Test500Error(t *C) { spool := mock.NewSpooler(nil) spool.FilesOut = []string{"file1", "file2", "file3"} spool.DataOut = map[string][]byte{ "file1": []byte("file1"), "file2": []byte("file2"), "file3": []byte("file3"), } sender := data.NewSender(s.logger, s.client) err := sender.Start(spool, s.tickerChan, 5, false) t.Assert(err, IsNil) s.tickerChan <- time.Now() got := test.WaitBytes(s.dataChan) if same, diff := test.IsDeeply(got[0], []byte("file1")); !same { t.Error(diff) } // 3 files before API error. t.Check(len(spool.DataOut), Equals, 3) // Simulate API error. select { case s.respChan <- &proto.Response{Code: 503}: case <-time.After(500 * time.Millisecond): t.Error("Sender receives prot.Response after sending data") } // Wait for it to finsih and return. if !test.WaitStatusPrefix(data.MAX_SEND_ERRORS*data.CONNECT_ERROR_WAIT, sender, "data-sender", "Idle") { t.Fatal("Timeout waiting for data-sender status=Idle") } // Still 3 files after API error. t.Check(len(spool.DataOut), Equals, 3) t.Check(len(spool.RejectedFiles), Equals, 0) // There's only 1 call to SendBytes because after an API error // the send stops immediately. trace := test.DrainTraceChan(s.client.TraceChan) t.Check(trace, DeepEquals, []string{ "ConnectOnce", "SendBytes", "Recv", "DisconnectOnce", }) err = sender.Stop() t.Assert(err, IsNil) }
func (s *SenderTestSuite) TestRecvErrors(t *C) { spool := mock.NewSpooler(nil) spool.FilesOut = []string{"slow001.json"} spool.DataOut = map[string][]byte{"slow001.json": []byte("...")} sender := data.NewSender(s.logger, s.client) err := sender.Start(spool, s.tickerChan, 60, false) t.Assert(err, IsNil) // Any recv error will do. doneChan := make(chan bool) go func() { for { select { case s.client.RecvError <- io.EOF: case <-doneChan: return } } }() defer func() { doneChan <- true }() // Tick causes send to connect and send all files. s.tickerChan <- time.Now() t0 := time.Now() // Wait for sender to finsih and return. if !test.WaitStatusPrefix(data.MAX_SEND_ERRORS*data.CONNECT_ERROR_WAIT, sender, "data-sender", "Idle") { t.Fatal("Timeout waiting for data-sender status=Idle") } d := time.Now().Sub(t0).Seconds() // It should wait between reconnects, but not too long. if d < float64((data.MAX_SEND_ERRORS-1)*data.CONNECT_ERROR_WAIT) { t.Error("Waits between reconnects") } if d > float64(data.MAX_SEND_ERRORS*data.CONNECT_ERROR_WAIT) { t.Error("Waited too long between reconnects") } err = sender.Stop() t.Assert(err, IsNil) // Didn't receive proper ack, so it doesn't remove any data. t.Check(len(spool.DataOut), Equals, 1) t.Check(len(spool.RejectedFiles), Equals, 0) // It should have called ConnectOnce() serveral times, else it didn't // really try to reconnect. trace := test.DrainTraceChan(s.client.TraceChan) t.Check(trace, DeepEquals, []string{ "ConnectOnce", "SendBytes", "Recv", "DisconnectOnce", "ConnectOnce", "SendBytes", "Recv", "DisconnectOnce", "ConnectOnce", "SendBytes", "Recv", "DisconnectOnce", "DisconnectOnce", }) }
func (s *SenderTestSuite) SetUpTest(t *C) { test.DrainTraceChan(s.client.TraceChan) test.DrainDataChan(s.dataChan) test.DrainRecvData(s.respChan) }