コード例 #1
0
func TestFileInfo(t *testing.T) {
	fname := path.Join(os.TempDir(), "binlog_streamer.test")
	writer, err := os.Create(fname)
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(fname)
	var file fileInfo
	err = file.Init(fname, 0)
	if err != nil {
		t.Fatal(err)
	}
	ch := make(chan []byte, 10)
	var svm = sync2.ServiceManager{}
	svm.Go(func(svc *sync2.ServiceContext) error {
		for svm.State() == sync2.SERVICE_RUNNING {
			file.WaitForChange(svc)
			b := make([]byte, 128)
			n, err := file.handle.Read(b)
			if err != nil {
				ch <- []byte(err.Error())
			}
			file.Set(file.lastPos + int64(n))
			ch <- b[:n]
		}
		return nil
	})

	want := "Message1"
	writer.WriteString(want)
	writer.Sync()
	got := string(<-ch)
	if want != got {
		t.Errorf("want %v, got %v", want, got)
	}

	want = "Message2"
	writer.WriteString(want)
	writer.Sync()
	got = string(<-ch)
	if want != got {
		t.Errorf("want %v, got %v", want, got)
	}

	time.Sleep(200 * time.Millisecond)
	want = "Message3"
	writer.WriteString(want)
	writer.Sync()
	got = string(<-ch)
	if want != got {
		t.Errorf("want %v, got %v", want, got)
	}

	want = "EOF"
	svm.Stop()
	got = string(<-ch)
	if want != got {
		t.Errorf("want %v, got %v", want, got)
	}
}
コード例 #2
0
ファイル: streamlog_test.go プロジェクト: chinna1986/vitess
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)
	}
}
コード例 #3
0
ファイル: streamlog_test.go プロジェクト: rn2dy/vitess
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)
	}
}