func BenchmarkEcho(b *testing.B) {
	b.StopTimer() // Initialize
	sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
	// Start server
	s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}
	defer s.Stop()
	// Start client
	proxy, err := NewRemoteAppConn(sockPath, SOCKET)
	if err != nil {
		Exit(err.Error())
	} else {
		b.Log("Connected")
	}
	echoString := strings.Repeat(" ", 200)
	b.StartTimer() // Start benchmarking tests

	for i := 0; i < b.N; i++ {
		proxy.EchoAsync(echoString)
	}
	proxy.FlushSync()

	b.StopTimer()
	// info := proxy.InfoSync()
	//b.Log("N: ", b.N, info)
}
func TestEcho(t *testing.T) {
	sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))

	// Start server
	s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}
	defer s.Stop()
	// Start client
	proxy, err := NewRemoteAppConn(sockPath, SOCKET)
	if err != nil {
		Exit(err.Error())
	} else {
		t.Log("Connected")
	}

	for i := 0; i < 1000; i++ {
		proxy.EchoAsync(Fmt("echo-%v", i))
	}
	proxy.FlushSync()
}
func TestInfo(t *testing.T) {
	sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
	// Start server
	s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}
	defer s.Stop()
	// Start client
	proxy, err := NewRemoteAppConn(sockPath, SOCKET)
	if err != nil {
		Exit(err.Error())
	} else {
		t.Log("Connected")
	}
	res := proxy.InfoSync()
	if res.IsErr() {
		t.Errorf("Unexpected error: %v", err)
	}
	if string(res.Data) != "size:0" {
		t.Error("Expected ResponseInfo with one element 'size:0' but got something else")
	}
}
Esempio n. 4
0
func testStream(t *testing.T, app types.Application) {

	numAppendTxs := 200000

	// Start the listener
	server, err := server.NewSocketServer("unix://test.sock", app)
	if err != nil {
		Exit(Fmt("Error starting socket server: %v", err.Error()))
	}
	defer server.Stop()

	// Connect to the socket
	client, err := tmspcli.NewSocketClient("unix://test.sock", false)
	if err != nil {
		Exit(Fmt("Error starting socket client: %v", err.Error()))
	}
	client.Start()
	defer client.Stop()

	done := make(chan struct{})
	counter := 0
	client.SetResponseCallback(func(req *types.Request, res *types.Response) {
		// Process response
		switch r := res.Value.(type) {
		case *types.Response_AppendTx:
			counter += 1
			if r.AppendTx.Code != types.CodeType_OK {
				t.Error("AppendTx failed with ret_code", r.AppendTx.Code)
			}
			if counter > numAppendTxs {
				t.Fatalf("Too many AppendTx responses. Got %d, expected %d", counter, numAppendTxs)
			}
			if counter == numAppendTxs {
				go func() {
					time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
					close(done)
				}()
				return
			}
		case *types.Response_Flush:
			// ignore
		default:
			t.Error("Unexpected response type", reflect.TypeOf(res.Value))
		}
	})

	// Write requests
	for counter := 0; counter < numAppendTxs; counter++ {
		// Send request
		reqRes := client.AppendTxAsync([]byte("test"))
		_ = reqRes
		// check err ?

		// Sometimes send flush messages
		if counter%123 == 0 {
			client.FlushAsync()
			// check err ?
		}
	}

	// Send final flush message
	client.FlushAsync()

	<-done
}