Esempio n. 1
0
func testBasic() {
	fmt.Println("Running basic tests")
	appProc := startApp()
	defer appProc.StopProcess(true)
	client := startClient()
	defer client.Stop()

	setOption(client, "serial", "on")
	commit(client, nil)
	appendTx(client, []byte("abc"), types.CodeType_BadNonce, nil)
	commit(client, nil)
	appendTx(client, []byte{0x00}, types.CodeType_OK, nil)
	commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 1})
	appendTx(client, []byte{0x00}, types.CodeType_BadNonce, nil)
	appendTx(client, []byte{0x01}, types.CodeType_OK, nil)
	appendTx(client, []byte{0x00, 0x02}, types.CodeType_OK, nil)
	appendTx(client, []byte{0x00, 0x03}, types.CodeType_OK, nil)
	appendTx(client, []byte{0x00, 0x00, 0x04}, types.CodeType_OK, nil)
	appendTx(client, []byte{0x00, 0x00, 0x06}, types.CodeType_BadNonce, nil)
	commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 5})
}
Esempio n. 2
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
}