Skip to content

Commit

Permalink
update Readme and add unit tests for rpc transport
Browse files Browse the repository at this point in the history
  • Loading branch information
danl5 committed Jun 11, 2024
1 parent 0fbb951 commit 1c5a148
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ clean:
@go clean
rm --force "xx.out"
test:
@go test
@go test ./...
check:
@go fmt ./
@go vet ./
Expand All @@ -29,4 +29,4 @@ help:
@echo "make cover - Checks the test coverage."
@echo "make run - Runs the program directly."
@echo "make lint - Performs code linting."
@echo "make docker - Builds a Docker image."
@echo "make docker - Builds a Docker image."
49 changes: 29 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,35 @@ type ElectConfig struct {

Create an Elect instance:
```go
e, err := goelect.NewElect(&goelect.ElectConfig{
ElectTimeout: 200,
HeartBeatInterval: 150,
ConnectTimeout: 10,
Peers: peerNodes,
// state transition callbacks
CallBacks: &goelect.StateCallBacks{
EnterLeader: enterLeader,
LeaveLeader: leaveLeader,
EnterFollower: enterFollower,
LeaveFollower: leaveFollower,
EnterCandidate: enterCandidate,
LeaveCandidate: leaveCandidate,
},
// self node
Node: goelect.Node{
Address: *nodeAddress,
ID: *nodeAddress,
},
}, slog.Default())
// use the built-in RPC as the transport layer.
rpcTransport, err := rpc.NewRPC(logger)
if err != nil {
return nil, err
}
e, err := goelect.NewElect(
rpcTransport,
// rpc transport config
&rpc.Config{},
&goelect.ElectConfig{
ElectTimeout: 200,
HeartBeatInterval: 150,
ConnectTimeout: 10,
Peers: peerNodes,
// state transition callbacks
CallBacks: &goelect.StateCallBacks{
EnterLeader: enterLeader,
LeaveLeader: leaveLeader,
EnterFollower: enterFollower,
LeaveFollower: leaveFollower,
EnterCandidate: enterCandidate,
LeaveCandidate: leaveCandidate,
},
// self node
Node: goelect.Node{
Address: *nodeAddress,
ID: *nodeAddress,
},
}, logger)
```
Start the Elect:
```go
Expand Down
15 changes: 2 additions & 13 deletions examples/onenode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,12 @@ func newElect() (*goelect.Elect, error) {
if err != nil {
return nil, err
}
// rpc transport config
rpcTransportConfig := &rpc.Config{
ServerCAs: nil,
ServerKey: "",
ServerCert: "",
ServerSkipVerify: false,
ClientCAs: nil,
ClientCert: "",
ClientKey: "",
ClientSkipVerify: false,
ConnectTimeout: 0,
}

// new elect
e, err := goelect.NewElect(
rpcTransport,
rpcTransportConfig,
// rpc transport config
&rpc.Config{},
&goelect.ElectConfig{
ElectTimeout: 200,
HeartBeatInterval: 150,
Expand Down
119 changes: 119 additions & 0 deletions pkg/transport/rpc/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package rpc

import (
"errors"
"testing"
)

func TestConfig_Validate(t *testing.T) {
// Define test cases
tests := []struct {
name string
config Config
expectedError error
}{
{
name: "incomplete server certificate configuration",
config: Config{
ServerKey: "key.pem",
ServerCert: "",
ServerSkipVerify: false,
ServerCAs: []string{},
ClientKey: "",
ClientCert: "",
ClientSkipVerify: false,
ClientCAs: []string{},
},
expectedError: errors.New("incomplete server certificate configuration"),
},
{
name: "no server CAs configured",
config: Config{
ServerKey: "cert.key",
ServerCert: "cert.pem",
ServerSkipVerify: false,
ServerCAs: []string{},
ClientKey: "",
ClientCert: "",
ClientSkipVerify: false,
ClientCAs: []string{},
},
expectedError: errors.New("no server CAs configured"),
},
{
name: "incomplete client certificate configuration",
config: Config{
ServerKey: "",
ServerCert: "",
ServerSkipVerify: false,
ServerCAs: []string{},
ClientKey: "key.pem",
ClientCert: "",
ClientSkipVerify: false,
ClientCAs: []string{},
},
expectedError: errors.New("incomplete client certificate configuration"),
},
{
name: "no client CAs configured",
config: Config{
ServerKey: "",
ServerCert: "",
ServerSkipVerify: false,
ServerCAs: []string{},
ClientKey: "cert.key",
ClientCert: "cert.pem",
ClientSkipVerify: false,
ClientCAs: []string{},
},
expectedError: errors.New("no client CAs configured"),
},
{
name: "valid configuration",
config: Config{
ServerKey: "key.pem",
ServerCert: "cert.pem",
ServerSkipVerify: true,
ServerCAs: []string{},
ClientKey: "client_key.pem",
ClientCert: "client_cert.pem",
ClientSkipVerify: true,
ClientCAs: []string{},
},
expectedError: nil,
},
{
name: "empty configuration",
config: Config{
ServerKey: "",
ServerCert: "",
ServerSkipVerify: false,
ServerCAs: []string{},
ClientKey: "",
ClientCert: "",
ClientSkipVerify: false,
ClientCAs: []string{},
},
expectedError: nil,
},
}

// Run test cases
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.Validate()

if tt.expectedError != nil && err == nil {
t.Errorf("expected error %v, but got nil", tt.expectedError)
}

if tt.expectedError == nil && err != nil {
t.Errorf("expected no error, but got %v", err)
}

if tt.expectedError != nil && err != nil && tt.expectedError.Error() != err.Error() {
t.Errorf("expected error %v, but got %v", tt.expectedError, err)
}
})
}
}

0 comments on commit 1c5a148

Please sign in to comment.