-
Notifications
You must be signed in to change notification settings - Fork 388
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 98be95b
Showing
2 changed files
with
96 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,95 @@ | ||
// 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" | ||
id2, addr2 := "svr-2", "127.0.0.2:8300" | ||
server1, server2 := buildServerParts(id1, addr1), buildServerParts(id2, addr2) | ||
|
||
// act | ||
lookup.AddServer(server1) | ||
lookup.AddServer(server2) | ||
|
||
// assert | ||
servers := lookup.Servers() | ||
require.EqualValuesf(t, 2, 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) | ||
require.Containsf(t, servers, server2, "Expected server %v to be in the list of servers", server2) | ||
|
||
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) | ||
|
||
got, err = lookup.ServerAddr(raft.ServerID(id2)) | ||
require.NoErrorf(t, err, "Unexpected error: %v", err) | ||
require.EqualValuesf(t, addr2, string(got), "Expected %v but got %v", addr2, got) | ||
|
||
server = lookup.Server(raft.ServerAddress(addr2)) | ||
strAddr = server.RPCAddr.String() | ||
require.EqualValuesf(t, addr2, strAddr, "Expected lookup to return address %v but got %v", addr2, strAddr) | ||
} | ||
|
||
func TestRemoveServer(t *testing.T) { | ||
// arrange | ||
lookup := NewServerLookup() | ||
id1, addr1 := "svr-1", "127.0.0.1:8300" | ||
id2, addr2 := "svr-2", "127.0.0.2:8300" | ||
server1, server2 := buildServerParts(id1, addr1), buildServerParts(id2, addr2) | ||
lookup.AddServer(server1) | ||
lookup.AddServer(server2) | ||
|
||
// act | ||
lookup.RemoveServer(server1) | ||
|
||
// assert | ||
servers := lookup.Servers() | ||
require.EqualValuesf(t, 1, len(servers), "Expected 0 servers but got %v", len(servers)) | ||
require.Containsf(t, servers, server2, "Expected server %v to be in the list of servers", server2) | ||
|
||
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) | ||
|
||
got, err := lookup.ServerAddr(raft.ServerID(id2)) | ||
require.NoErrorf(t, err, "Unexpected error: %v", err) | ||
require.EqualValuesf(t, addr2, string(got), "Expected %v but got %v", addr2, got) | ||
|
||
server := lookup.Server(raft.ServerAddress(addr2)) | ||
strAddr := server.RPCAddr.String() | ||
require.EqualValuesf(t, addr2, strAddr, "Expected lookup to return address %v but got %v", addr2, strAddr) | ||
} | ||
|
||
func buildServerParts(id, addr string) *ServerParts { | ||
return &ServerParts{ | ||
ID: id, | ||
RPCAddr: &testAddr{addr}, | ||
} | ||
} |