func TestInfo(t *testing.T) {
	sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
	_, err := server.StartListener(sockPath, example.NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}
	conn, err := Connect(sockPath)
	if err != nil {
		Exit(err.Error())
	} else {
		t.Log("Connected")
	}

	logBuffer := bytes.NewBuffer(nil)
	logConn := logio.NewLoggedConn(conn, logBuffer)
	proxy := NewRemoteAppContext(logConn, 10)
	proxy.Start()
	data, err := proxy.InfoSync()
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}
	if data[0] != "size:0" {
		t.Error("Expected ResponseInfo with one element 'size:0' but got something else")
	}
}
func TestEcho(t *testing.T) {
	sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
	_, err := server.StartListener(sockPath, example.NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}
	conn, err := Connect(sockPath)
	if err != nil {
		Exit(err.Error())
	} else {
		t.Log("Connected")
	}

	logBuffer := bytes.NewBuffer(nil)
	logConn := logio.NewLoggedConn(conn, logBuffer)
	proxy := NewRemoteAppContext(logConn, 10)
	proxy.SetResponseCallback(nil)
	proxy.Start()

	for i := 0; i < 1000; i++ {
		proxy.EchoAsync(Fmt("echo-%v", i))
	}
	proxy.FlushSync()

	/*
		if t.Failed() {
			logio.PrintReader(logBuffer)
		}
	*/
}
func BenchmarkEcho(b *testing.B) {
	b.StopTimer() // Initialize
	sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
	_, err := server.StartListener(sockPath, example.NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}
	conn, err := Connect(sockPath)
	if err != nil {
		Exit(err.Error())
	} else {
		b.Log("Connected")
	}

	proxy := NewRemoteAppContext(conn, 10)
	proxy.Start()
	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)
}
Exemplo n.º 4
0
func TestNodeStartStop(t *testing.T) {

	// Start a dummy app
	go func() {
		_, err := server.StartListener(config.GetString("proxy_app"), example.NewDummyApplication())
		if err != nil {
			Exit(err.Error())
		}
	}()
	// wait for the server
	time.Sleep(time.Second * 2)

	// Create & start node
	n := NewNode()
	l := p2p.NewDefaultListener("tcp", config.GetString("node_laddr"), config.GetBool("skip_upnp"))
	n.AddListener(l)
	n.Start()
	log.Notice("Started node", "nodeInfo", n.sw.NodeInfo())
	time.Sleep(time.Second * 2)
	ch := make(chan struct{}, 1)
	go func() {
		n.Stop()
		ch <- struct{}{}
	}()
	ticker := time.NewTicker(time.Second * 5)
	select {
	case <-ch:
	case <-ticker.C:
		t.Fatal("timed out waiting for shutdown")
	}
}
Exemplo n.º 5
0
func run(ctx *cli.Context) {
	// Start the tmsp listener
	// all flags are passed through to the app
	_, err := server.StartListener("tcp://0.0.0.0:46658", gethapp.NewEthereumApplication(ctx))
	if err != nil {
		Exit(err.Error())
	}

	// Wait forever
	TrapSignal(func() {
		// TODO: Cleanup
	})
}
Exemplo n.º 6
0
func main() {

	// Start the listener
	_, err := server.StartListener("tcp://0.0.0.0:46658", example.NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}

	// Wait forever
	TrapSignal(func() {
		// Cleanup
	})

}
Exemplo n.º 7
0
func main() {

	addrPtr := flag.String("address", "tcp://0.0.0.0:46658", "Listen address")
	eyesPtr := flag.String("eyes", "tcp://0.0.0.0:46659", "MerkleEyes address")
	genPtr := flag.String("genesis", "genesis.json", "Genesis JSON file")
	flag.Parse()

	// Connect to MerkleEyes
	eyesCli, err := eyes.NewMerkleEyesClient(*eyesPtr)
	if err != nil {
		Exit("connect to MerkleEyes: " + err.Error())
	}

	// Create BlackStar app
	app := app.NewBlackstar(eyesCli)

	// Load GenesisState
	jsonBytes, err := ReadFile(*genPtr)
	if err != nil {
		Exit("read genesis: " + err.Error())
	}
	genesisState := types.GenesisState{}
	wire.ReadJSONPtr(&genesisState, jsonBytes, &err)
	if err != nil {
		Exit("parsing genesis JSON: " + err.Error())
	}
	for _, account := range genesisState.Accounts {
		// pubKeyBytes := wire.BinaryBytes(account.PubKey)
		pubKeyString := account.PubKey.KeyString()
		accBytes := wire.BinaryBytes(account.Account)
		err = eyesCli.SetSync([]byte(pubKeyString), accBytes)
		if err != nil {
			Exit("loading genesis accounts: " + err.Error())
		}
	}

	// Start the listener
	_, err = server.StartListener(*addrPtr, app)
	if err != nil {
		Exit("create listener: " + err.Error())
	}

	// Wait forever
	TrapSignal(func() {
		// Cleanup
	})

}
Exemplo n.º 8
0
Arquivo: main.go Projeto: marleyg/tmsp
func main() {

	addrPtr := flag.String("addr", "tcp://0.0.0.0:46658", "Listen address")
	flag.Parse()

	// Start the listener
	_, err := server.StartListener(*addrPtr, dummy.NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}

	// Wait forever
	TrapSignal(func() {
		// Cleanup
	})

}
Exemplo n.º 9
0
func main() {

	serialPtr := flag.Bool("serial", false, "Enforce incrementing (serial) txs")
	flag.Parse()
	app := example.NewCounterApplication(*serialPtr)

	// Start the listener
	_, err := server.StartListener("tcp://0.0.0.0:46658", app)
	if err != nil {
		Exit(err.Error())
	}

	// Wait forever
	TrapSignal(func() {
		// Cleanup
	})

}
Exemplo n.º 10
0
func main() {

	var govFile string
	if len(os.Args) == 1 {
		Exit("Please specify the governmint genesis file")
	}

	govFile = os.Args[1]

	// Start the listener
	_, err := server.StartListener("tcp://0.0.0.0:46658", gov.NewGovernmintApplication(govFile))
	if err != nil {
		Exit(err.Error())
	}

	// Wait forever
	TrapSignal(func() {
		// Cleanup
	})

}
Exemplo n.º 11
0
func TestStream(t *testing.T) {

	numAppendTxs := 200000

	// Start the listener
	_, err := server.StartListener("tcp://127.0.0.1:46658", NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}

	// Connect to the socket
	conn, err := Connect("tcp://127.0.0.1:46658")
	if err != nil {
		Exit(err.Error())
	}

	// Read response data
	done := make(chan struct{})
	go func() {
		counter := 0
		for {
			var n int
			var err error
			var res types.Response
			wire.ReadBinaryPtrLengthPrefixed(&res, conn, 0, &n, &err)
			if err != nil {
				Exit(err.Error())
			}

			// Process response
			switch res := res.(type) {
			case types.ResponseAppendTx:
				counter += 1
				if res.RetCode != types.RetCodeOK {
					t.Error("AppendTx failed with ret_code", res.RetCode)
				}
				if counter > numAppendTxs {
					t.Fatal("Too many AppendTx responses")
				}
				t.Log("response", counter)
				if counter == numAppendTxs {
					go func() {
						time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
						close(done)
					}()
				}
			case types.ResponseFlush:
				// ignore
			default:
				t.Error("Unexpected response type", reflect.TypeOf(res))
			}
		}
	}()

	// Write requests
	for counter := 0; counter < numAppendTxs; counter++ {
		// Send request
		var n int
		var err error
		var req types.Request = types.RequestAppendTx{TxBytes: []byte("test")}
		wire.WriteBinaryLengthPrefixed(struct{ types.Request }{req}, conn, &n, &err)
		if err != nil {
			t.Fatal(err.Error())
		}

		// Sometimes send flush messages
		if counter%123 == 0 {
			t.Log("flush")
			wire.WriteBinaryLengthPrefixed(struct{ types.Request }{types.RequestFlush{}}, conn, &n, &err)
			if err != nil {
				t.Fatal(err.Error())
			}
		}
	}

	// Send final flush message
	var n int
	wire.WriteBinaryLengthPrefixed(struct{ types.Request }{types.RequestFlush{}}, conn, &n, &err)
	if err != nil {
		t.Fatal(err.Error())
	}

	<-done
}
Exemplo n.º 12
0
func TestStream(t *testing.T) {

	numAppendTxs := 200000

	// Start the listener
	_, err := server.StartListener("tcp://127.0.0.1:46658", NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}

	// Connect to the socket
	conn, err := Connect("tcp://127.0.0.1:46658")
	if err != nil {
		Exit(err.Error())
	}

	// Read response data
	done := make(chan struct{})
	go func() {
		counter := 0
		for {

			var res = &types.Response{}
			err := types.ReadMessage(conn, res)
			if err != nil {
				Exit(err.Error())
			}

			// Process response
			switch res.Type {
			case types.MessageType_AppendTx:
				counter += 1
				if res.Code != types.CodeType_OK {
					t.Error("AppendTx failed with ret_code", res.Code)
				}
				if counter > numAppendTxs {
					t.Fatal("Too many AppendTx responses")
				}
				t.Log("response", counter)
				if counter == numAppendTxs {
					go func() {
						time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
						close(done)
					}()
				}
			case types.MessageType_Flush:
				// ignore
			default:
				t.Error("Unexpected response type", res.Type)
			}
		}
	}()

	// Write requests
	for counter := 0; counter < numAppendTxs; counter++ {
		// Send request
		var req = types.RequestAppendTx([]byte("test"))
		err := types.WriteMessage(req, conn)
		if err != nil {
			t.Fatal(err.Error())
		}

		// Sometimes send flush messages
		if counter%123 == 0 {
			t.Log("flush")
			err := types.WriteMessage(types.RequestFlush(), conn)
			if err != nil {
				t.Fatal(err.Error())
			}
		}
	}

	// Send final flush message
	err = types.WriteMessage(types.RequestFlush(), conn)
	if err != nil {
		t.Fatal(err.Error())
	}

	<-done
}