func TestChannel(t *testing.T) { logger := New("logger", 1) lastValue := sync2.AtomicString{} svm := sync2.ServiceManager{} svm.Go(func(svc *sync2.ServiceContext) error { ch := logger.Subscribe() defer logger.Unsubscribe(ch) for svc.IsRunning() { lastValue.Set((<-ch).(*logMessage).Format(nil)) } return nil }) time.Sleep(10 * time.Millisecond) if sz := logger.size.Get(); sz != 1 { t.Errorf("want 1, got %d", sz) } logger.Send(&logMessage{"val2"}) time.Sleep(10 * time.Millisecond) if lastValue.Get() != "val2\n" { t.Errorf("want val2\\n, got %q", lastValue.Get()) } go logger.Send(&logMessage{"val3"}) svm.Stop() time.Sleep(10 * time.Millisecond) if sz := logger.size.Get(); sz != 0 { t.Errorf("want 0, got %d", sz) } }
func TestChannel(t *testing.T) { logger := New("logger", 1) lastValue := sync2.AtomicString{} svm := sync2.ServiceManager{} svm.Go(func(_ *sync2.ServiceManager) { ch := logger.Subscribe(nil) defer logger.Unsubscribe(ch) for svm.State() == sync2.SERVICE_RUNNING { lastValue.Set(<-ch) } }) time.Sleep(10 * time.Millisecond) if sz := logger.size.Get(); sz != 1 { t.Errorf("want 1, got %d", sz) } logger.Send(&logMessage{"val2"}) time.Sleep(10 * time.Millisecond) if lastValue.Get() != "val2\n" { t.Errorf("want val2\\n, got %q", lastValue.Get()) } go logger.Send(&logMessage{"val3"}) svm.Stop() time.Sleep(10 * time.Millisecond) if sz := logger.size.Get(); sz != 0 { t.Errorf("want 0, got %d", sz) } }
func TestHTTP(t *testing.T) { l, err := net.Listen("tcp", ":0") if err != nil { t.Fatal(err) } addr := l.Addr().String() go http.Serve(l, nil) logger := New("logger", 1) logger.ServeLogs("/log", func(params url.Values, x interface{}) string { return x.(*logMessage).Format(params) }) // This should not block logger.Send(&logMessage{"val1"}) lastValue := sync2.AtomicString{} svm := sync2.ServiceManager{} svm.Go(func(svc *sync2.ServiceContext) error { resp, err := http.Get(fmt.Sprintf("http://%s/log", addr)) if err != nil { t.Fatal(err) } defer resp.Body.Close() buf := make([]byte, 100) for svc.IsRunning() { n, err := resp.Body.Read(buf) if err != nil { t.Fatal(err) } lastValue.Set(string(buf[:n])) } return nil }) time.Sleep(100 * time.Millisecond) if sz := logger.size.Get(); sz != 1 { t.Errorf("want 1, got %d", sz) } logger.Send(&logMessage{"val2"}) time.Sleep(100 * time.Millisecond) if lastValue.Get() != "val2\n" { t.Errorf("want val2\\n, got %q", lastValue.Get()) } // This part of the test is flaky. // Uncomment for one-time testing. /* // This send will unblock the http client // which will allow it to see the stop request // from svm. go logger.Send(&logMessage{"val3"}) svm.Stop() // You have to send a few times before the writer // returns an error. for i := 0; i < 10; i++ { time.Sleep(100 * time.Millisecond) logger.Send(&logMessage{"val4"}) } time.Sleep(100 * time.Millisecond) if sz := logger.size.Get(); sz != 0 { t.Errorf("want 0, got %d", sz) } */ }