func Test_Sender(t *testing.T) { var wg sync.WaitGroup wg.Add(1) metrics := []byte("name.space 1.0 132131\n") ts := tcptest.NewServer(func(conn net.Conn) { defer conn.Close() bs, err := bufio.NewReader(conn).ReadBytes('\n') if err != nil { t.Error(err) } if !bytes.Equal(bs, metrics) { t.Error(fmt.Errorf("send and receive not match")) } wg.Done() }) metricCh := make(chan []byte, 1) ctx := context.Background() NewSender(ctx, "tcp", ts.Addr.String(), metricCh) metricCh <- metrics wg.Wait() }
func TestCheckTCPExecute(t *testing.T) { ts := tcptest.NewServer(func(conn net.Conn) { defer conn.Close() // do nothing }) defer ts.Close() c := CheckTCP{Addr: ts.Addr.String()} err := c.Execute(1 * time.Second) if err != nil { t.Fatal("unexpected error:", err) } }
func testFetch(t *testing.T, fixture []byte) { ts := tcptest.NewServer(func(conn net.Conn) { defer conn.Close() conn.Write(fixture) }) defer ts.Close() ctx := context.Background() fetchCnt := 30000 fetchSignal := make(chan struct{}, 1) metricCh := make(chan []byte, 0) NewFetcher(ctx, "tcp", ts.Addr.String(), fetchSignal, metricCh, fetchCnt, "test", false, FetchBufSize, time.Minute, "") fetchSignal <- struct{}{} var wg sync.WaitGroup wg.Add(1) go func() { metrics := <-metricCh metricLines := bytes.Split(metrics, []byte("\n")) for _, m := range metricLines { if len(m) <= 0 { continue } parts := bytes.Split(m, []byte(" ")) if len(parts) != 3 { t.Errorf("metric format is wrong m:%v", string(m)) t.Logf("%v", string(m)) } // t.Logf("%v", string(m)) } wg.Done() }() wg.Wait() }