Esempio n. 1
0
func testSimpleEvent(t *testing.T, factory clientFactory) {
	enableLogging([]string{"*"})
	mock := transptest.NewMockServerTCP(t, 1*time.Second, "", nil)
	server, _ := v2.NewWithListener(mock.Listener)
	defer server.Close()

	transp, err := mock.Connect()
	if err != nil {
		t.Fatalf("Failed to connect: %v", err)
	}
	client := factory(transp)
	defer transp.Close()
	defer client.Stop()

	event := common.MapStr{"type": "test", "name": "me", "line": 10}
	go client.Publish([]common.MapStr{event})

	// try to receive event from server
	batch := server.Receive()
	batch.ACK()

	// validate
	events := batch.Events
	assert.Equal(t, 1, len(events))
	msg := events[0].(map[string]interface{})
	assert.Equal(t, "me", msg["name"])
	assert.Equal(t, 10.0, msg["line"])
}
Esempio n. 2
0
func testCloseAfterWindowSize(t *testing.T, factory clientFactory) {
	enableLogging([]string{"*"})
	server := transptest.NewMockServerTCP(t, 100*time.Millisecond, "", nil)

	sock, transp, err := server.ConnectPair()
	if err != nil {
		t.Fatalf("Failed to connect server and client: %v", err)
	}
	client := factory(transp)
	conn := &mockConn{sock, streambuf.New(nil)}
	defer transp.Close()
	defer sock.Close()
	defer client.Stop()

	client.Publish([]common.MapStr{common.MapStr{
		"type":    "test",
		"message": "hello world",
	}})

	_, err = conn.recvMessage()
	if err != nil {
		t.Fatalf("failed to read window size message: %v", err)
	}

}
Esempio n. 3
0
func testSendZero(t *testing.T, factory clientFactory) {
	enableLogging([]string{"*"})

	server := transptest.NewMockServerTCP(t, 1*time.Second, "", nil)
	defer server.Close()

	sock, transp, err := server.ConnectPair()
	if err != nil {
		t.Fatalf("Failed to connect server and client: %v", err)
	}

	client := factory(transp)
	defer sock.Close()
	defer transp.Close()

	client.Publish(make([]common.MapStr, 0))

	client.Stop()
	returns := client.Returns()

	assert.Equal(t, 1, len(returns))
	if len(returns) == 1 {
		assert.Equal(t, 0, returns[0].n)
		assert.Nil(t, returns[0].err)
	}
}
Esempio n. 4
0
func TestLogstashTCP(t *testing.T) {
	timeout := 2 * time.Second
	server := transptest.NewMockServerTCP(t, timeout, "", nil)

	// create lumberjack output client
	config := map[string]interface{}{
		"hosts":   []string{server.Addr()},
		"index":   testLogstashIndex("logstash-conn-tcp"),
		"timeout": 2,
	}
	testConnectionType(t, server, testOutputerFactory(t, "", config))
}
Esempio n. 5
0
func testStructuredEvent(t *testing.T, factory clientFactory) {
	enableLogging([]string{"*"})
	server := transptest.NewMockServerTCP(t, 1*time.Second, "", nil)

	sock, transp, err := server.ConnectPair()
	if err != nil {
		t.Fatalf("Failed to connect server and client: %v", err)
	}
	client := factory(transp)
	conn := &mockConn{sock, streambuf.New(nil)}
	defer transp.Close()
	defer sock.Close()

	event := common.MapStr{
		"type": "test",
		"name": "test",
		"struct": common.MapStr{
			"field1": 1,
			"field2": true,
			"field3": []int{1, 2, 3},
			"field4": []interface{}{
				1,
				"test",
				common.MapStr{
					"sub": "field",
				},
			},
			"field5": common.MapStr{
				"sub1": 2,
			},
		},
	}
	client.Publish([]common.MapStr{event})

	win, err := conn.recvMessage()
	assert.Nil(t, err)

	msg, err := conn.recvMessage()
	assert.Nil(t, err)

	conn.sendACK(1)
	defer client.Stop()

	// validate
	assert.NotNil(t, win)
	assert.NotNil(t, msg)
	assert.Equal(t, 1, len(msg.events))
	msg = msg.events[0]
	assert.Equal(t, "test", msg.doc["name"])
	assert.Equal(t, 1.0, msg.doc.get("struct.field1"))
	assert.Equal(t, true, msg.doc.get("struct.field2"))
	assert.Equal(t, 2.0, msg.doc.get("struct.field5.sub1"))
}
Esempio n. 6
0
func testStructuredEvent(t *testing.T, factory clientFactory) {
	enableLogging([]string{"*"})
	mock := transptest.NewMockServerTCP(t, 1*time.Second, "", nil)
	server, _ := v2.NewWithListener(mock.Listener)
	defer server.Close()

	transp, err := mock.Connect()
	if err != nil {
		t.Fatalf("Failed to connect: %v", err)
	}
	client := factory(transp)
	defer transp.Close()
	defer client.Stop()

	event := common.MapStr{
		"type": "test",
		"name": "test",
		"struct": common.MapStr{
			"field1": 1,
			"field2": true,
			"field3": []int{1, 2, 3},
			"field4": []interface{}{
				1,
				"test",
				common.MapStr{
					"sub": "field",
				},
			},
			"field5": common.MapStr{
				"sub1": 2,
			},
		},
	}
	go client.Publish([]common.MapStr{event})
	defer client.Stop()

	// try to receive event from server
	batch := server.Receive()
	batch.ACK()

	events := batch.Events
	assert.Equal(t, 1, len(events))
	msg := events[0]
	assert.Equal(t, "test", eventGet(msg, "name"))
	assert.Equal(t, 1.0, eventGet(msg, "struct.field1"))
	assert.Equal(t, true, eventGet(msg, "struct.field2"))
	assert.Equal(t, 2.0, eventGet(msg, "struct.field5.sub1"))
}
Esempio n. 7
0
func testMultiFailMaxTimeouts(t *testing.T, factory clientFactory) {
	enableLogging([]string{"*"})

	mock := transptest.NewMockServerTCP(t, 100*time.Millisecond, "", nil)
	server, _ := v2.NewWithListener(mock.Listener)
	defer server.Close()

	transp, err := mock.Transp()
	if err != nil {
		t.Fatalf("Failed to connect: %v", err)
	}
	client := factory(transp)
	defer transp.Close()
	defer client.Stop()

	N := 8
	event := outputs.Data{Event: common.MapStr{"type": "test", "name": "me", "line": 10}}

	for i := 0; i < N; i++ {
		// reconnect client
		client.Close()
		client.Connect()

		// publish event. With client returning on timeout, we have to send
		// messages again
		go client.Publish([]outputs.Data{event})

		// read batch + never ACK in order to enforce timeout
		server.Receive()

		// wait for max connection timeout ensuring ACK receive fails
		time.Sleep(100 * time.Millisecond)
	}

	client.Stop()
	returns := client.Returns()
	if len(returns) != N {
		t.Fatalf("PublishEvents did not return")
	}

	for _, ret := range returns {
		assert.Equal(t, 0, ret.n)
		assert.NotNil(t, ret.err)
	}
}
Esempio n. 8
0
func testSimpleEvent(t *testing.T, factory clientFactory) {
	enableLogging([]string{"*"})
	server := transptest.NewMockServerTCP(t, 1*time.Second, "", nil)

	sock, transp, err := server.ConnectPair()
	if err != nil {
		t.Fatalf("Failed to connect server and client: %v", err)
	}
	client := factory(transp)
	conn := &mockConn{sock, streambuf.New(nil)}
	defer transp.Close()
	defer sock.Close()

	event := common.MapStr{"type": "test", "name": "me", "line": 10}
	client.Publish([]common.MapStr{event})

	// receive window message
	err = sock.SetReadDeadline(time.Now().Add(1 * time.Second))
	win, err := conn.recvMessage()
	assert.Nil(t, err)

	// receive data message
	msg, err := conn.recvMessage()
	assert.Nil(t, err)

	// send ack
	conn.sendACK(1)

	client.Stop()

	// validate
	assert.NotNil(t, win)
	assert.NotNil(t, msg)
	assert.Equal(t, 1, len(msg.events))
	msg = msg.events[0]
	assert.Equal(t, "me", msg.doc["name"])
	assert.Equal(t, 10.0, msg.doc["line"])
}
Esempio n. 9
0
func newClientServerTCP(t *testing.T, to time.Duration) *clientServer {
	return &clientServer{transptest.NewMockServerTCP(t, to, "", nil)}
}
Esempio n. 10
0
func newMockTCPServer(t *testing.T, to time.Duration) *mockLSServer {
	return &mockLSServer{transptest.NewMockServerTCP(t, to, "", nil)}
}
Esempio n. 11
0
func newProtoServerTCP(t *testing.T, to time.Duration) *protocolServer {
	return &protocolServer{transptest.NewMockServerTCP(t, to, "", nil)}
}
Esempio n. 12
0
func testMultiFailMaxTimeouts(t *testing.T, factory clientFactory) {
	enableLogging([]string{"*"})

	server := transptest.NewMockServerTCP(t, 100*time.Millisecond, "", nil)
	transp, err := server.Transp()
	if err != nil {
		t.Fatalf("Failed to connect server and client: %v", err)
	}

	N := 8
	client := factory(transp)
	defer transp.Close()
	defer client.Stop()

	event := common.MapStr{"type": "test", "name": "me", "line": 10}

	for i := 0; i < N; i++ {
		await := server.Await()
		err = transp.Connect()
		if err != nil {
			t.Fatalf("Transport client Failed to connect: %v", err)
		}
		sock := <-await
		conn := &mockConn{sock, streambuf.New(nil)}

		// close socket only once test has finished
		// so no EOF error can be generated
		defer sock.Close()

		// publish event. With client returning on timeout, we have to send
		// messages again
		client.Publish([]common.MapStr{event})

		// read window
		msg, err := conn.recvMessage()
		if err != nil {
			t.Errorf("Failed receiving window size: %v", err)
			break
		}
		if msg.code != 'W' {
			t.Errorf("expected window size message")
			break
		}

		// read message
		msg, err = conn.recvMessage()
		if err != nil {
			t.Errorf("Failed receiving data message: %v", err)
			break
		}
		if msg.code != 'C' {
			t.Errorf("expected data message")
			break
		}
		// do not respond -> enforce timeout

		// check connection being closed,
		// timeout required in case of sender not closing the connection
		// correctly
		sock.SetDeadline(time.Now().Add(30 * time.Second))
		msg, err = conn.recvMessage()
		if msg != nil {
			t.Errorf("Received message on connection expected to be closed")
			break
		}
		if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
			t.Errorf("Unexpected timeout error (client did not close connection in time?): %v", err)
			break
		}
	}

	client.Stop()

	returns := client.Returns()
	if len(returns) != N {
		t.Fatalf("PublishEvents did not return")
	}

	for _, ret := range returns {
		assert.Equal(t, 0, ret.n)
		assert.NotNil(t, ret.err)
	}
}