func (s *SenderTestSuite) TestSendData(t *C) { spool := mock.NewSpooler(nil) slow001, err := ioutil.ReadFile(sample + "slow001.json") if err != nil { t.Fatal(err) } spool.FilesOut = []string{"slow001.json"} spool.DataOut = map[string][]byte{"slow001.json": slow001} sender := data.NewSender(s.logger, s.client) err = sender.Start(spool, s.tickerChan, 5, false) if err != nil { t.Fatal(err) } data := test.WaitBytes(s.dataChan) if len(data) != 0 { t.Errorf("No data sent before tick; got %+v", data) } s.tickerChan <- time.Now() data = test.WaitBytes(s.dataChan) if same, diff := test.IsDeeply(data[0], slow001); !same { t.Error(diff) } t.Check(len(spool.DataOut), Equals, 1) select { case s.respChan <- &proto.Response{Code: 200}: case <-time.After(500 * time.Millisecond): t.Error("Sender receives prot.Response after sending data") } // Sender should include its websocket client status. We're using a mock ws client // which reports itself as "data-client: ok". status := sender.Status() t.Check(status["data-client"], Equals, "ok") err = sender.Stop() t.Assert(err, IsNil) t.Check(len(spool.DataOut), Equals, 0) t.Check(len(spool.RejectedFiles), Equals, 0) }
func (s *SenderTestSuite) TestSendEmptyFile(t *C) { // Make mock spooler which returns a single file name and zero bytes // for that file. spool := mock.NewSpooler(nil) spool.FilesOut = []string{"empty.json"} spool.DataOut = map[string][]byte{"empty.json": []byte{}} // Start the sender. sender := data.NewSender(s.logger, s.client) err := sender.Start(spool, s.tickerChan, 5, false) t.Assert(err, IsNil) // Tick to make sender send. s.tickerChan <- time.Now() // Sender shouldn't zero-length data files... data := test.WaitBytes(s.dataChan) t.Check(data, HasLen, 0) err = sender.Stop() t.Assert(err, IsNil) // ...but it should remove them. t.Check(len(spool.DataOut), Equals, 0) }
func (s *SenderTestSuite) TestBlackhole(t *C) { spool := mock.NewSpooler(nil) slow001, err := ioutil.ReadFile(sample + "slow001.json") if err != nil { t.Fatal(err) } spool.FilesOut = []string{"slow001.json"} spool.DataOut = map[string][]byte{"slow001.json": slow001} sender := data.NewSender(s.logger, s.client) err = sender.Start(spool, s.tickerChan, 5, true) // <- true = enable blackhole if err != nil { t.Fatal(err) } s.tickerChan <- time.Now() data := test.WaitBytes(s.dataChan) if len(data) != 0 { t.Errorf("Data sent despite blackhole; got %+v", data) } select { case s.respChan <- &proto.Response{Code: 200}: // Should not recv response because no data was sent. t.Error("Sender receives prot.Response after sending data") case <-time.After(500 * time.Millisecond): } err = sender.Stop() t.Assert(err, IsNil) }
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) }