本文整理匯總了Golang中github.com/elastic/beats/libbeat/outputs/transport/transptest.NewMockServerTCP函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewMockServerTCP函數的具體用法?Golang NewMockServerTCP怎麽用?Golang NewMockServerTCP使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewMockServerTCP函數的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: testSimpleEvent
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"])
}
示例2: testCloseAfterWindowSize
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)
}
}
示例3: testSendZero
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)
}
}
示例4: TestLogstashTCP
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))
}
示例5: testStructuredEvent
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"))
}
示例6: testStructuredEvent
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"))
}
示例7: testMultiFailMaxTimeouts
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)
}
}
示例8: testSimpleEvent
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"])
}
示例9: newClientServerTCP
func newClientServerTCP(t *testing.T, to time.Duration) *clientServer {
return &clientServer{transptest.NewMockServerTCP(t, to, "", nil)}
}
示例10: newMockTCPServer
func newMockTCPServer(t *testing.T, to time.Duration) *mockLSServer {
return &mockLSServer{transptest.NewMockServerTCP(t, to, "", nil)}
}
示例11: newProtoServerTCP
func newProtoServerTCP(t *testing.T, to time.Duration) *protocolServer {
return &protocolServer{transptest.NewMockServerTCP(t, to, "", nil)}
}
示例12: testMultiFailMaxTimeouts
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)
}
}