Skip to content

Commit

Permalink
Unit tests for pitaya package
Browse files Browse the repository at this point in the history
  • Loading branch information
felipejfc committed Apr 10, 2018
1 parent cf77d46 commit f4847f1
Show file tree
Hide file tree
Showing 12 changed files with 744 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ protos-compile:
@cd protos && protoc --gogofaster_out=. pitaya.proto

rm-test-temp-files:
@rm -f cluster/127.0.0.1*
@rm -f cluster/localhost*
@rm -f cluster/127.0.0.1* 127.0.0.1*
@rm -f cluster/localhost* localhost*

test:
@go test $(TESTABLE_PACKAGES)
Expand Down
141 changes: 132 additions & 9 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
package pitaya

import (
"fmt"
"net"
"os"
"reflect"
"testing"
"time"

Expand All @@ -30,23 +34,50 @@ import (
"github.com/topfreegames/pitaya/acceptor"
"github.com/topfreegames/pitaya/cluster"
"github.com/topfreegames/pitaya/constants"
"github.com/topfreegames/pitaya/helpers"
"github.com/topfreegames/pitaya/internal/codec"
"github.com/topfreegames/pitaya/internal/message"
"github.com/topfreegames/pitaya/route"
"github.com/topfreegames/pitaya/router"
"github.com/topfreegames/pitaya/serialize/json"
"github.com/topfreegames/pitaya/session"
"github.com/topfreegames/pitaya/timer"
)

var tables = []struct {
isFrontend bool
serverType string
serverMode ServerMode
serverMetadata map[string]string
cfg *viper.Viper
}{
{true, "sv1", Cluster, map[string]string{"name": "bla"}, viper.New()},
{false, "sv2", Standalone, map[string]string{}, viper.New()},
var (
tables = []struct {
isFrontend bool
serverType string
serverMode ServerMode
serverMetadata map[string]string
cfg *viper.Viper
}{
{true, "sv1", Cluster, map[string]string{"name": "bla"}, viper.New()},
{false, "sv2", Standalone, map[string]string{}, viper.New()},
}
typeOfetcdSD reflect.Type
typeOfNatsRPCServer reflect.Type
typeOfNatsRPCClient reflect.Type
)

func TestMain(m *testing.M) {
setup()
exit := m.Run()
os.Exit(exit)
}

func setup() {
initApp()
Configure(true, "testtype", Cluster, map[string]string{}, viper.New())

etcdSD, _ := cluster.NewEtcdServiceDiscovery(app.config, app.server)
typeOfetcdSD = reflect.TypeOf(etcdSD)

natsRPCServer, _ := cluster.NewNatsRPCServer(app.config, app.server)
typeOfNatsRPCServer = reflect.TypeOf(natsRPCServer)

natsRPCClient, _ := cluster.NewNatsRPCClient(app.config, app.server)
typeOfNatsRPCClient = reflect.TypeOf(natsRPCClient)
}

func initApp() {
Expand Down Expand Up @@ -216,3 +247,95 @@ func TestShutdown(t *testing.T) {
}()
<-app.dieChan
}

func TestStartDefaultSD(t *testing.T) {
initApp()
Configure(true, "testtype", Cluster, map[string]string{}, viper.New())
startDefaultSD()
assert.NotNil(t, app.serviceDiscovery)
assert.Equal(t, typeOfetcdSD, reflect.TypeOf(app.serviceDiscovery))
}

func TestStartDefaultRPCServer(t *testing.T) {
initApp()
Configure(true, "testtype", Cluster, map[string]string{}, viper.New())
startDefaultRPCServer()
assert.NotNil(t, app.rpcServer)
assert.Equal(t, typeOfNatsRPCServer, reflect.TypeOf(app.rpcServer))
}

func TestStartDefaultRPCClient(t *testing.T) {
initApp()
Configure(true, "testtype", Cluster, map[string]string{}, viper.New())
startDefaultRPCClient()
assert.NotNil(t, app.rpcClient)
assert.Equal(t, typeOfNatsRPCClient, reflect.TypeOf(app.rpcClient))
}

func TestStartAndListenStandalone(t *testing.T) {
initApp()
Configure(true, "testtype", Standalone, map[string]string{}, viper.New())

acc := acceptor.NewTCPAcceptor("0.0.0.0:0")
AddAcceptor(acc)

go func() {
Start()
}()
helpers.ShouldEventuallyReturn(t, func() bool {
return app.running
}, true)

assert.NotNil(t, handlerService)
assert.NotNil(t, timer.GlobalTicker)
// should be listening
assert.NotEmpty(t, acc.GetAddr())
helpers.ShouldEventuallyReturn(t, func() error {
n, err := net.Dial("tcp", acc.GetAddr())
defer n.Close()
return err
}, nil, 10*time.Millisecond, 100*time.Millisecond)
}

func ConfigureClusterApp() {

}

func TestStartAndListenCluster(t *testing.T) {
es, cli := helpers.GetTestEtcd(t)
defer es.Terminate(t)

ns := helpers.GetTestNatsServer(t)
nsAddr := ns.Addr().String()

cfg := viper.New()
cfg.Set("pitaya.cluster.rpc.client.nats.connect", fmt.Sprintf("nats://%s", nsAddr))
cfg.Set("pitaya.cluster.rpc.server.nats.connect", fmt.Sprintf("nats://%s", nsAddr))

initApp()
Configure(true, "testtype", Cluster, map[string]string{}, cfg)

etcdSD, err := cluster.NewEtcdServiceDiscovery(app.config, app.server, cli)
assert.NoError(t, err)
SetServiceDiscoveryClient(etcdSD)

acc := acceptor.NewTCPAcceptor("0.0.0.0:0")
AddAcceptor(acc)

go func() {
Start()
}()
helpers.ShouldEventuallyReturn(t, func() bool {
return app.running
}, true)

assert.NotNil(t, handlerService)
assert.NotNil(t, timer.GlobalTicker)
// should be listening
assert.NotEmpty(t, acc.GetAddr())
helpers.ShouldEventuallyReturn(t, func() error {
n, err := net.Dial("tcp", acc.GetAddr())
defer n.Close()
return err
}, nil, 10*time.Millisecond, 100*time.Millisecond)
}
30 changes: 11 additions & 19 deletions cluster/etcd_service_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"time"

"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/integration"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/topfreegames/pitaya/config"
Expand All @@ -48,13 +47,6 @@ func getConfig(conf ...*viper.Viper) *config.Config {
return config
}

func getEtcd(t *testing.T) (*integration.ClusterV3, *clientv3.Client) {
t.Helper()
c := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
cli := c.RandClient()
return c, cli
}

func getEtcdSD(t *testing.T, config *config.Config, server *Server, cli *clientv3.Client) *etcdServiceDiscovery {
t.Helper()
e, err := NewEtcdServiceDiscovery(config, server, cli)
Expand All @@ -67,7 +59,7 @@ func TestNewEtcdServiceDiscovery(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := getConfig()
c, cli := getEtcd(t)
c, cli := helpers.GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, config, table.server, cli)
assert.NotNil(t, e)
Expand All @@ -80,7 +72,7 @@ func TestEtcdSDBootstrapLease(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := getConfig()
c, cli := getEtcd(t)
c, cli := helpers.GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, config, table.server, cli)
err := e.bootstrapLease()
Expand All @@ -95,7 +87,7 @@ func TestEtcdSDBootstrapLeaseError(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := getConfig()
c, cli := getEtcd(t)
c, cli := helpers.GetTestEtcd(t)
c.Terminate(t)
e := getEtcdSD(t, config, table.server, cli)
err := e.bootstrapLease()
Expand All @@ -109,7 +101,7 @@ func TestEtcdSDBootstrapServer(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := getConfig()
c, cli := getEtcd(t)
c, cli := helpers.GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, config, table.server, cli)
e.bootstrapLease()
Expand All @@ -134,7 +126,7 @@ func TestEtcdSDDeleteServer(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := getConfig()
c, cli := getEtcd(t)
c, cli := helpers.GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, config, table.server, cli)
e.bootstrapLease()
Expand Down Expand Up @@ -174,7 +166,7 @@ func TestEtcdSDDeleteLocalInvalidServers(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := getConfig()
_, cli := getEtcd(t)
_, cli := helpers.GetTestEtcd(t)
e := getEtcdSD(t, config, table.server, cli)
invalidServer := &Server{
ID: "invalid",
Expand All @@ -194,7 +186,7 @@ func TestEtcdSDGetServer(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := getConfig()
c, cli := getEtcd(t)
c, cli := helpers.GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, config, table.server, cli)
e.bootstrapLease()
Expand All @@ -214,7 +206,7 @@ func TestEtcdSDInit(t *testing.T) {
conf.Set("pitaya.cluster.sd.etcd.heartbeat.interval", "30ms")
conf.Set("pitaya.cluster.sd.etcd.syncservers.interval", "30ms")
config := getConfig(conf)
c, cli := getEtcd(t)
c, cli := helpers.GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, config, table.server, cli)
e.Init()
Expand Down Expand Up @@ -244,7 +236,7 @@ func TestEtcdShutdown(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := getConfig()
c, cli := getEtcd(t)
c, cli := helpers.GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, config, table.server, cli)
e.Init()
Expand All @@ -264,7 +256,7 @@ func TestEtcdWatchChangesAddNewServers(t *testing.T) {
conf := viper.New()
conf.Set("pitaya.cluster.sd.etcd.syncservers.interval", "10ms")
config := getConfig(conf)
c, cli := getEtcd(t)
c, cli := helpers.GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, config, table.server, cli)
e.running = true
Expand Down Expand Up @@ -298,7 +290,7 @@ func TestEtcdWatchChangesDeleteServers(t *testing.T) {
conf := viper.New()
conf.Set("pitaya.cluster.sd.etcd.syncservers.interval", "10ms")
config := getConfig(conf)
c, cli := getEtcd(t)
c, cli := helpers.GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, config, table.server, cli)
e.running = true
Expand Down
87 changes: 87 additions & 0 deletions component_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) nano Author and TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package pitaya

import (
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/topfreegames/pitaya/component"
)

type MyComp struct {
component.Base
running bool
}

func (m *MyComp) Init() {
m.running = true
}

func (m *MyComp) Shutdown() {
m.running = false
}

func resetComps() {
handlerComp = make([]regComp, 0)
remoteComp = make([]regComp, 0)
}

func TestRegister(t *testing.T) {
resetComps()
b := &component.Base{}
Register(b)
assert.Equal(t, 1, len(handlerComp))
assert.Equal(t, regComp{b, nil}, handlerComp[0])
}

func TestRegisterRemote(t *testing.T) {
resetComps()
b := &component.Base{}
RegisterRemote(b)
assert.Equal(t, 1, len(remoteComp))
assert.Equal(t, regComp{b, nil}, remoteComp[0])
}

func TestStartupComponents(t *testing.T) {
initApp()
resetComps()
Configure(true, "testtype", Standalone, map[string]string{}, viper.New())

Register(&MyComp{})
RegisterRemote(&MyComp{})
startupComponents()
assert.Equal(t, true, handlerComp[0].comp.(*MyComp).running)
}

func TestShutdownComponents(t *testing.T) {
resetComps()
initApp()
Configure(true, "testtype", Standalone, map[string]string{}, viper.New())

Register(&MyComp{})
RegisterRemote(&MyComp{})
startupComponents()

shutdownComponents()
assert.Equal(t, false, handlerComp[0].comp.(*MyComp).running)
}
1 change: 1 addition & 0 deletions constants/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ var (
ErrSessionNotFound = errors.New("session not found")
ErrSessionOnNotify = errors.New("current session working on notify mode")
ErrWrongValueType = errors.New("protobuf: convert on wrong type value")
ErrNilCondition = errors.New("pitaya/timer: nil condition")
)

0 comments on commit f4847f1

Please sign in to comment.