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 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 #3
0
func main() {
	// Create a new server.
	server, err := tcp.NewServer("127.0.0.1:42193")
	if err != nil {
		log.Fatalln(err)
	}

	// Set the handler function.
	server.OnNewSocket(onNewSocket)

	// Log.
	log.Println("Server listening...")

	// Start the server.
	server.Listen()
}
Example #4
0
func TestServerClose(t *testing.T) {
	server, err := tcp.NewServer("127.0.0.1:34349")
	require.NoError(t, err)
	require.NotNil(t, server)

	// Timeout.
	go func() {
		time.Sleep(3 * time.Second)

		if !server.IsClosed() {
			t.Fatal("failed to close server within timeout")
		}
	}()

	go func() {
		time.Sleep(time.Second)
		server.Close()
	}()

	server.Listen()
}
Example #5
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 #6
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 #7
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()
}