// TestRedirects ensures that the socket follows redirects, if they are given. func TestRedirects(tester *testing.T) { t := test.New(tester) count1 := 0 count2 := 0 listener1, err := net.Listen("tcp", ":11111") t.AssertNil(err, "net.Listen") listener2, err := net.Listen("tcp", ":11112") t.AssertNil(err, "net.Listen") server1 := &http.Server{Handler: websocket.Handler(redirect(&count1))} server2 := &http.Server{Handler: websocket.Handler(accept(&count2))} go server1.Serve(listener1) go server2.Serve(listener2) socket := &Socket{ HostPort: "localhost:11111", Path: "", Origin: "localhost:12345", } _, err = socket.Send(nil, 3, fakeOrigin) t.AssertNil(err, "socket.Send") listener1.Close() listener2.Close() matcher := new(test.IntMatcher) t.AssertEqual(matcher, 1, count1) t.AssertEqual(matcher, 1, count2) }
// TestRetries ensures that the socket retries a few times before giving up. func TestRetries(tester *testing.T) { t := test.New(tester) requestCount := 0 listener, err := net.Listen("tcp", ":11111") t.AssertNil(err, "net.Listen") server := &http.Server{ Handler: websocket.Handler(count(&requestCount)), ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, } go server.Serve(listener) socket := &Socket{ HostPort: "localhost:11111", Path: "", Origin: "localhost:12345", } attempts := 5 _, err = socket.Send(nil, attempts, fakeOrigin) t.AssertNotNil(err, "socket.Send") listener.Close() matcher := new(test.IntMatcher) t.AssertEqual(matcher, attempts, requestCount) }
// TestReceive ensures that the socket can read from the connection. func TestReceive(tester *testing.T) { t := test.New(tester) listener, err := net.Listen("tcp", ":11111") t.AssertNil(err, "net.Listen") server := &http.Server{Handler: websocket.Handler(numbers)} go server.Serve(listener) socket := &Socket{ HostPort: "localhost:11111", Path: "", Origin: fakeOrigin, } request := new(Sync) _, err = socket.Send(request, 5, fakeOrigin) t.AssertNil(err, "socket.Send") for i := 1; i <= 10; i++ { ack := new(Ack) err = socket.Receive(ack) t.AssertNil(err, "socket.Receive") val, _ := binary.Varint(ack.Payload) t.AssertEqual(new(test.IntMatcher), i, int(val)) } listener.Close() }
// TestFailure ensures that Send returns an error on failure. func TestFailure(tester *testing.T) { t := test.New(tester) requestCount := 0 listener, err := net.Listen("tcp", ":11111") t.AssertNil(err, "net.Listen") server := &http.Server{ Handler: websocket.Handler(fail(&requestCount)), } go server.Serve(listener) socket := &Socket{ HostPort: "localhost:11111", Path: "", Origin: "localhost:12345", } _, err = socket.Send(nil, 3, fakeOrigin) t.AssertNotNil(err, "socket.Send") listener.Close() t.AssertEqual(new(test.IntMatcher), 1, requestCount) }
// TestResetReceive ensures that Receives are interrupted. func TestResetReceive(tester *testing.T) { t := test.New(tester) count := 0 listener, err := net.Listen("tcp", ":11111") t.AssertNil(err, "net.Listen") server := &http.Server{Handler: websocket.Handler(accept(&count))} go server.Serve(listener) socket := &Socket{ HostPort: "localhost:11111", Path: "", Origin: fakeOrigin, } request := new(Sync) _, err = socket.Send(request, 5, fakeOrigin) t.AssertNil(err, "socket.Send") go func() { time.Sleep(1 * time.Second) socket.Reset("localhost:11112") }() err = socket.Receive(new(Ack)) t.AssertNotNil(err, "socket.Receive") listener.Close() }
// TestOpenFromEnd ensures that we can open a log file at its tail. func TestOpenFromEnd(tester *testing.T) { config := newTestConfig() t := test.New(tester) log, err := OpenLog(config, "temp", 0) t.AssertNil(err, "OpenLog") defer os.Remove(log.Name()) var i byte for i = 1; i <= 10; i++ { payload := []byte{i} message := &protocol.Message{int64(i), payload, crc32.ChecksumIEEE(payload)} _, err := log.Append("x", message) t.AssertNil(err, "log.Append") } log.Close() log, err = OpenLog(config, "temp", -1) t.AssertNil(err, "OpenLog") _, err = log.ReadNext() if err != io.EOF { t.Error("Error should be EOF.") } t.AssertTrue(log.IsEOF(), "log.IsEOF") log.Close() }
// TestDuplicateDetection ensures that duplicate requests from the same // producer are not written to the log. func TestDuplicateDetection(tester *testing.T) { config := newTestConfig() t := test.New(tester) log, err := OpenLog(config, "temp", 0) t.AssertNil(err, "OpenLog") defer os.Remove(log.Name()) var i byte for i = 1; i <= 10; i++ { payload := []byte{i} message := &protocol.Message{1, payload, crc32.ChecksumIEEE(payload)} _, err := log.Append("x", message) t.AssertNil(err, "log.Append") } log.Close() log, err = OpenLog(config, "temp", 0) t.AssertNil(err, "OpenLog") entry, err := log.ReadNext() t.AssertEqual(new(test.IntMatcher), 1, int(entry.Payload[0])) _, err = log.ReadNext() if err != io.EOF { t.Error("Error should be EOF.") } log.Close() }
// TestTruncateLog ensures that truncateLog cuts the log at the given point. func TestTruncateLog(tester *testing.T) { config := newTestConfig() t := test.New(tester) log, err := OpenLog(config, "temp", 0) t.AssertNil(err, "OpenLog") defer os.Remove(log.Name()) var i byte for i = 1; i <= 10; i++ { payload := []byte{i} message := &protocol.Message{int64(i), payload, crc32.ChecksumIEEE(payload)} _, err := log.Append("x", message) t.AssertNil(err, "log.Append") } log.Close() truncateLog(config, "temp", 5*41) log, err = OpenLog(config, "temp", 0) t.AssertNil(err, "OpenLog") for i = 1; i <= 5; i++ { entry, err := log.ReadNext() t.AssertNil(err, "log.ReadNext()") t.AssertEqual(new(test.IntMatcher), int(i), int(entry.Payload[0])) } t.AssertTrue(log.IsEOF(), "log.IsEOF") log.Close() }
// TestOpenError ensures that OpenLog returns an error if the file cannot be // opened. func TestOpenError(tester *testing.T) { config := newTestConfig() config.Base = "/some_non_existing_dir_on_your_machine" t := test.New(tester) _, err := OpenLog(config, "temp", 0) t.AssertNotNil(err, "OpenLog") }
// TestResetSend ensures that Sends are interrupted. func TestResetSend(tester *testing.T) { t := test.New(tester) count1 := 0 count2 := 0 listener1, err := net.Listen("tcp", ":11111") t.AssertNil(err, "net.Listen") listener2, err := net.Listen("tcp", ":11112") t.AssertNil(err, "net.Listen") server1 := &http.Server{Handler: websocket.Handler(count(&count1))} server2 := &http.Server{Handler: websocket.Handler(accept(&count2))} go server1.Serve(listener1) go server2.Serve(listener2) socket := &Socket{ HostPort: "localhost:11111", Path: "", Origin: fakeOrigin, } go socket.Send(nil, math.MaxInt32, fakeOrigin) time.Sleep(1 * time.Second) socket.Reset("localhost:11112") time.Sleep(1 * time.Second) listener1.Close() listener2.Close() t.AssertPositive(int64(count1), "socket.Send") t.AssertEqual(new(test.IntMatcher), 1, count2) }