forked from distribworks/dkron
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add simple server_lookup_test.go & small fix (use ID instead of name …
…in server_lookup.go)
- Loading branch information
1 parent
986ef63
commit f9c445b
Showing
2 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package dkron | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/raft" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testAddr struct { | ||
addr string | ||
} | ||
|
||
func (ta *testAddr) Network() string { | ||
return "tcp" | ||
} | ||
|
||
func (ta *testAddr) String() string { | ||
return ta.addr | ||
} | ||
|
||
func TestAddServer(t *testing.T) { | ||
// arrange | ||
lookup := NewServerLookup() | ||
id1, addr1 := "svr-1", "127.0.0.1:8300" | ||
server1 := buildServerParts(id1, addr1) | ||
|
||
// act | ||
lookup.AddServer(server1) | ||
|
||
// assert | ||
servers := lookup.Servers() | ||
require.EqualValuesf(t, 1, len(servers), "Expected 1 servers but got %v", len(servers)) | ||
require.Containsf(t, servers, server1, "Expected server %v to be in the list of servers", server1) | ||
|
||
got, err := lookup.ServerAddr(raft.ServerID(id1)) | ||
require.NoErrorf(t, err, "Unexpected error: %v", err) | ||
require.EqualValuesf(t, addr1, string(got), "Expected %v but got %v", addr1, got) | ||
|
||
server := lookup.Server(raft.ServerAddress(addr1)) | ||
strAddr := server.RPCAddr.String() | ||
require.EqualValuesf(t, addr1, strAddr, "Expected lookup to return address %v but got %v", addr1, strAddr) | ||
} | ||
|
||
func TestRemoveServer(t *testing.T) { | ||
// arrange | ||
lookup := NewServerLookup() | ||
id1, addr1 := "svr-1", "127.0.0.1:8300" | ||
server1 := buildServerParts(id1, addr1) | ||
lookup.AddServer(server1) | ||
|
||
// act | ||
lookup.RemoveServer(server1) | ||
|
||
// assert | ||
servers := lookup.Servers() | ||
require.EqualValuesf(t, 0, len(servers), "Expected 0 servers but got %v", len(servers)) | ||
|
||
require.Nilf(t, lookup.Server(raft.ServerAddress(addr1)), "Expected lookup to return nil") | ||
addr, err := lookup.ServerAddr(raft.ServerID(id1)) | ||
require.Errorf(t, err, "Expected lookup to return error") | ||
require.EqualValuesf(t, "", string(addr), "Expected empty address but got %v", addr) | ||
} | ||
|
||
func buildServerParts(id, addr string) *ServerParts { | ||
return &ServerParts{ | ||
ID: id, | ||
RPCAddr: &testAddr{addr}, | ||
} | ||
} |