Example #1
0
func TestSocketClose(t *testing.T) {
	var wg sync.WaitGroup

	server, err := tcp.NewServer("127.0.0.1:45355")
	require.NoError(t, err)
	require.NotNil(t, server)

	must := func(ok bool, args ...interface{}) {
		if ok {
			return
		}

		wg.Done()
		t.Fatal(args...)
	}

	wg.Add(3)

	go func() {
		server.Listen()
	}()

	go func() {
		c, err := tcp.NewClient("127.0.0.1:45355")
		must(err == nil, "client")
		must(c != nil, "client")

		c.OnClose(func(s *pakt.Socket) {
			must(c == s)
			wg.Done()
		})

		c.OnClose(func(s *pakt.Socket) {
			must(c == s)
			wg.Done()
		})

		// Start a goroutine for race test.
		go func() {
			for {
				if c.IsClosed() {
					return
				}
			}
		}()

		must(!c.IsClosed())
		c.Close()
		must(c.IsClosed())

		wg.Done()
	}()

	wg.Wait()
	server.Close()
}
Example #2
0
func main() {
	// Create a new client.
	s, err := tcp.NewClient("127.0.0.1:42193")
	if err != nil {
		log.Fatalln(err)
	}

	// Optional set the timeout for a call request.
	// s.SetCallTimeout(time.Minute)

	// Optional set the maximum message size.
	// s.SetMaxMessageSize(100 * 1024)

	// Set a function which is triggered as soon as the socket closed.
	// Optionally use the s.ClosedChan channel.
	s.OnClose(func(s *pakt.Socket) {
		// ...
	})

	// Register a remote callable function.
	// Optionally use s.RegisterFuncs to register multiple functions at once.
	s.RegisterFunc("bar", bar)

	// Signalize the socket that initialization is done.
	// Start accepting remote requests.
	s.Ready()

	// Create a dummy value.
	data := struct {
		A, B string
		C    int
	}{
		A: "Hallo",
		B: "Welt",
		C: 2408234082374023,
	}

	// Call the remote server function and obtain the return value.
	c, err := s.Call("foo", data)
	if err != nil {
		log.Fatalln(err)
	}

	// Decode the return value.
	err = c.Decode(&data)
	if err != nil {
		log.Fatalln(err)
	}

	// Log.
	log.Printf("received return data from server: %+v", data)

	// Exit the application as soon as the socket connection closes.
	<-s.ClosedChan()
}
Example #3
0
func TestSocketTimeout(t *testing.T) {
	var wg sync.WaitGroup

	server, err := tcp.NewServer("127.0.0.1:45355")
	require.NoError(t, err)
	require.NotNil(t, server)

	must := func(ok bool, args ...interface{}) {
		if ok {
			return
		}

		wg.Done()
		t.Fatal(args...)
	}

	wg.Add(1)

	server.OnNewSocket(func(s *pakt.Socket) {
		s.SetCallTimeout(2 * time.Second)
		s.Ready()

		_, err := s.Call("timeout")
		must(err == pakt.ErrTimeout, err)

		_, err = s.Call("timeout", nil, 5*time.Second)
		must(err == nil, err)

		wg.Done()
	})

	go func() {
		server.Listen()
	}()

	go func() {
		c, err := tcp.NewClient("127.0.0.1:45355")
		must(err == nil, "client")
		must(c != nil, "client")

		c.RegisterFunc("timeout", func(c *pakt.Context) (interface{}, error) {
			time.Sleep(3 * time.Second)
			return nil, nil
		})

		c.Ready()
	}()

	wg.Wait()

	server.Close()
}
Example #4
0
func TestServerSocketsMap(t *testing.T) {
	var wg sync.WaitGroup

	server, err := tcp.NewServer("127.0.0.1:45356")
	require.NoError(t, err)
	require.NotNil(t, server)

	must := func(ok bool, args ...interface{}) {
		if ok {
			return
		}

		wg.Done()
		t.Fatal(args...)
	}

	server.OnNewSocket(func(s *pakt.Socket) {
		s.SetCallTimeout(2 * time.Second)

		s.OnClose(func(s *pakt.Socket) {
			wg.Done()
		})

		s.Ready()

		_, err := s.Call("exit")
		must(err == nil, err)
	})

	go func() {
		server.Listen()
	}()

	for i := 0; i < 100; i++ {
		wg.Add(1)

		go func() {
			cl, err := tcp.NewClient("127.0.0.1:45356")
			must(err == nil, "client")
			must(cl != nil, "client")

			cl.RegisterFunc("exit", func(c *pakt.Context) (interface{}, error) {
				go func() {
					time.Sleep(500 * time.Millisecond)
					c.Socket().Close()
				}()
				return nil, nil
			})

			cl.Ready()
		}()
	}

	wg.Wait()

	time.Sleep(time.Second)

	require.Len(t, server.Sockets(), 0)

	server.Close()
}
Example #5
0
func TestServerMultipleSockets(t *testing.T) {
	type data struct {
		A string
		B string
		C int
	}

	var wg sync.WaitGroup

	server, err := tcp.NewServer("127.0.0.1:45354")
	require.NoError(t, err)
	require.NotNil(t, server)

	must := func(ok bool, args ...interface{}) {
		if ok {
			return
		}

		wg.Done()
		t.Fatal(args...)
	}

	server.OnNewSocket(func(s *pakt.Socket) {
		wg.Add(1)

		s.SetCallTimeout(5 * time.Second)

		s.RegisterFunc("greet", func(c *pakt.Context) (interface{}, error) {
			var s string
			err := c.Decode(&s)
			must(err == nil, "greet")
			must(s == "Greet", "greet")
			return "Roger", nil
		})

		s.Ready()

		ss := server.GetSocket(s.ID())
		must(ss != nil, "GetSocket")
		must(len(ss.ID()) > 0, "GetSocket")
		must(ss.ID() == s.ID(), "GetSocket")

		d := data{
			A: "Hallo",
			B: "Welt",
			C: 2408234082374023,
		}

		for i := 0; i < 100; i++ {
			c, err := s.Call("call", d)
			must(err == nil, "call request: ", err)

			err = c.Decode(&d)
			must(err == nil, "call request: decode")
			must(d.A == "Hallo", "call request: decode")
			must(d.B == "Welt", "call request: decode")
			must(d.C == 2408234082374023, "call request: decode")

			c, err = s.Call("err")
			must(err != nil, "call err: err != nil:", err)
			must(err.Error() == "ERROR", "call err: err != 'ERROR'", err.Error())
			err = c.Decode(&d)
			must(err != nil, "call err: decode:", err)
		}

		wg.Done()
	})

	go func() {
		server.Listen()
	}()

	for i := 0; i < 100; i++ {
		wg.Add(1)

		go func() {
			c, err := tcp.NewClient("127.0.0.1:45354")
			must(err == nil, "client")
			must(c != nil, "client")

			c.SetCallTimeout(5 * time.Second)

			c.RegisterFunc("call", func(c *pakt.Context) (interface{}, error) {
				var d data
				err := c.Decode(&d)
				must(err == nil, "client: call")
				must(d.A == "Hallo", "client: call")
				must(d.B == "Welt", "client: call")
				must(d.C == 2408234082374023, "client: call")

				return d, nil
			})

			c.RegisterFunc("err", func(c *pakt.Context) (interface{}, error) {
				return nil, fmt.Errorf("ERROR")
			})

			c.Ready()

			var s string
			for i := 0; i < 100; i++ {
				cc, err := c.Call("greet", "Greet")
				must(err == nil, "client: call greet: ", err)
				err = cc.Decode(&s)
				must(err == nil, "client: call greet")
				must(s == "Roger", "client: call greet")
			}

			wg.Done()
		}()
	}

	wg.Wait()

	server.Close()
	time.Sleep(time.Second)
	require.Len(t, server.Sockets(), 0)
}
Example #6
0
func TestSocketMultipleRoutines(t *testing.T) {
	type data struct {
		A string
		B string
		C int
	}

	var wg sync.WaitGroup

	server, err := tcp.NewServer("127.0.0.1:45356")
	require.NoError(t, err)
	require.NotNil(t, server)

	must := func(ok bool, args ...interface{}) {
		if ok {
			return
		}

		wg.Done()
		t.Fatal(args...)
	}

	server.OnNewSocket(func(s *pakt.Socket) {
		wg.Add(1)

		s.SetCallTimeout(3 * time.Second)

		s.RegisterFunc("greet", func(c *pakt.Context) (interface{}, error) {
			var s string
			err := c.Decode(&s)
			must(err == nil, "greet")
			must(s == "Greet", "greet")
			return "Roger", nil
		})

		s.Ready()

		d := data{
			A: "Hallo",
			B: "Welt",
			C: 2408234082374023,
		}

		for i := 0; i < 10000; i++ {
			wg.Add(1)

			go func() {
				c, err := s.Call("call", d)
				must(err == nil, "call request: ", err)

				var dd data
				err = c.Decode(&dd)
				must(err == nil, "call request: decode")
				must(dd.A == "Hallo", "call request: decode")
				must(dd.B == "Welt", "call request: decode")
				must(dd.C == 2408234082374023, "call request: decode")

				wg.Done()
			}()
		}

		wg.Done()
	})

	go func() {
		server.Listen()
	}()

	wg.Add(1)

	go func() {
		c, err := tcp.NewClient("127.0.0.1:45356")
		must(err == nil, "client")
		must(c != nil, "client")

		c.SetCallTimeout(3 * time.Second)

		c.RegisterFunc("call", func(c *pakt.Context) (interface{}, error) {
			var d data
			err := c.Decode(&d)
			must(err == nil, "client: call")
			must(d.A == "Hallo", "client: call")
			must(d.B == "Welt", "client: call")
			must(d.C == 2408234082374023, "client: call")

			return d, nil
		})

		c.Ready()

		for i := 0; i < 10000; i++ {
			wg.Add(1)

			go func() {
				cc, err := c.Call("greet", "Greet")
				must(err == nil, "client: call greet: ", err)

				var s string
				err = cc.Decode(&s)
				must(err == nil, "client: call greet")
				must(s == "Roger", "client: call greet")

				wg.Done()
			}()
		}

		wg.Done()
	}()

	wg.Wait()

	server.Close()
}