Skip to content

Commit

Permalink
feat: add some tests (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tochemey authored Jun 23, 2023
1 parent 2872dcd commit c954283
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 13 deletions.
8 changes: 0 additions & 8 deletions discovery/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ import (
"context"
)

type Method int

const (
KUBERNETES Method = iota
MDNS
LOCAL
)

// Discovery helps discover other running actor system in a cloud environment
type Discovery interface {
// ID returns the discovery name
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/prometheus/client_golang v1.16.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
github.com/travisjeffery/go-dynaport v1.0.0
go.etcd.io/etcd/api/v3 v3.5.9
go.etcd.io/etcd/client/v3 v3.5.9
go.etcd.io/etcd/raft/v3 v3.5.9
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 h1:6fotK7otjonDflCTK0BCfls4SPy3NcCVb5dqqmbRknE=
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75/go.mod h1:KO6IkyS8Y3j8OdNO85qEYBsRPuteD+YciPomcXdrMnk=
github.com/travisjeffery/go-dynaport v1.0.0 h1:m/qqf5AHgB96CMMSworIPyo1i7NZueRsnwdzdCJ8Ajw=
github.com/travisjeffery/go-dynaport v1.0.0/go.mod h1:0LHuDS4QAx+mAc4ri3WkQdavgVoBIZ7cE9ob17KIAJk=
github.com/xiang90/probing v0.0.0-20221125231312-a49e3df8f510 h1:S2dVYn90KE98chqDkyE9Z4N61UnQd+KOfgp5Iu53llk=
github.com/xiang90/probing v0.0.0-20221125231312-a49e3df8f510/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
4 changes: 1 addition & 3 deletions pkg/etcd/embed/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type Node struct {
config *Config
isStopped bool
isReady bool
isLeader bool

mu sync.Mutex

Expand All @@ -41,7 +40,6 @@ func NewNode(config *Config) *Node {
config: config,
isStopped: false,
isReady: false,
isLeader: false,
mu: sync.Mutex{},
logger: config.Logger(),
}
Expand Down Expand Up @@ -193,7 +191,7 @@ func (n *Node) IsLeader() bool {
n.mu.Lock()
// release the lock once done
defer n.mu.Unlock()
return n.server.Server.Leader().String() == n.server.Server.ID().String() && n.isLeader
return n.server.Server.Leader().String() == n.server.Server.ID().String()
}

// LeaderID returns the leader id
Expand Down
104 changes: 104 additions & 0 deletions pkg/etcd/embed/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package embed

import (
"fmt"
"os"
"testing"

"github.com/coreos/etcd/pkg/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tochemey/goakt/log"
"github.com/travisjeffery/go-dynaport"
)

func TestEmbed(t *testing.T) {
t.Run("With successful start and stop", func(t *testing.T) {
assert.NoError(t, os.RemoveAll("test"))

// let us generate two ports
ports := dynaport.Get(2)
clientsPort := ports[0]
peersPort := ports[1]

// create the various URLs and cluster name
clientURLs := types.MustNewURLs([]string{fmt.Sprintf("http://0.0.0.0:%d", clientsPort)})
peerURLs := types.MustNewURLs([]string{fmt.Sprintf("http://0.0.0.0:%d", peersPort)})
endpoints := clientURLs
clusterName := "test"
datadir := "test"

// create an instance of the config
config := NewConfig(clusterName, clientURLs, peerURLs, endpoints, WithDataDir(datadir))
// create an instance of node
node := NewNode(config)

// start the node server
require.NoError(t, node.Start())
assert.NotEmpty(t, node.ID())
members, err := node.Members()
require.NoError(t, err)
assert.NotEmpty(t, members)
assert.Len(t, members, 1)
assert.NotNil(t, node.Session())
assert.NotNil(t, node.Client())

// assertions single node cluster
curls := node.ClientURLs().StringSlice()
require.ElementsMatch(t, clientURLs.StringSlice(), curls)
require.True(t, node.IsLeader())
assert.NotEmpty(t, node.LeaderID())

// stop the node server
assert.NoError(t, node.Stop())
assert.NoError(t, os.RemoveAll("test"))
})
t.Run("With successful start and stop with initial URL without joining an existing cluster", func(t *testing.T) {
assert.NoError(t, os.RemoveAll("test"))
// let us generate two ports
ports := dynaport.Get(2)
clientsPort := ports[0]
peersPort := ports[1]

// create the various URLs and cluster name
clientURLs := types.MustNewURLs([]string{fmt.Sprintf("http://0.0.0.0:%d", clientsPort)})
peerURLs := types.MustNewURLs([]string{fmt.Sprintf("http://0.0.0.0:%d", peersPort)})

endpoints := clientURLs
clusterName := "test"
datadir := "test"
// create a logger
logger := log.New(log.DebugLevel, os.Stdout)

// create an instance of the config
config := NewConfig(clusterName,
clientURLs,
peerURLs,
endpoints,
WithDataDir(datadir),
WithInitialCluster(fmt.Sprintf("test=http://0.0.0.0:%d", peersPort)),
WithLogger(logger))
// create an instance of node
node := NewNode(config)

// start the node server
require.NoError(t, node.Start())
assert.NotEmpty(t, node.ID())
members, err := node.Members()
require.NoError(t, err)
assert.NotEmpty(t, members)
assert.Len(t, members, 1)
assert.NotNil(t, node.Session())
assert.NotNil(t, node.Client())

// assertions single node cluster
curls := node.ClientURLs().StringSlice()
require.ElementsMatch(t, clientURLs.StringSlice(), curls)
require.True(t, node.IsLeader())
assert.NotEmpty(t, node.LeaderID())

// stop the node server
assert.NoError(t, node.Stop())
assert.NoError(t, os.RemoveAll("test"))
})
}
11 changes: 9 additions & 2 deletions pkg/etcd/kvstore/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kvstore

import (
"context"
"fmt"
"os"
"testing"
"time"
Expand All @@ -11,16 +12,22 @@ import (
"github.com/stretchr/testify/require"
"github.com/tochemey/goakt/log"
"github.com/tochemey/goakt/pkg/etcd/embed"
"github.com/travisjeffery/go-dynaport"
)

func TestKVStore(t *testing.T) {
assert.NoError(t, os.RemoveAll("test"))
// create a context
ctx := context.TODO()

// let us generate two ports
ports := dynaport.Get(2)
clientsPort := ports[0]
peersPort := ports[1]

// create the various URLs and cluster name
clientURLs := types.MustNewURLs([]string{"http://0.0.0.0:2379"})
peerURLs := types.MustNewURLs([]string{"http://0.0.0.0:2380"})
clientURLs := types.MustNewURLs([]string{fmt.Sprintf("http://0.0.0.0:%d", clientsPort)})
peerURLs := types.MustNewURLs([]string{fmt.Sprintf("http://0.0.0.0:%d", peersPort)})
endpoints := clientURLs
clusterName := "test"
datadir := "test"
Expand Down

0 comments on commit c954283

Please sign in to comment.