-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clients/horizonclient: fix theoretical bug in currentServerTime (#4596)
address the case where the host name provided to currentServerTime isn't contained in the global ServerTimeMap.
- Loading branch information
1 parent
7db1e26
commit 35023ac
Showing
3 changed files
with
53 additions
and
3 deletions.
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,44 @@ | ||
package horizonclient | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCurrentServerTime(t *testing.T) { | ||
t.Run("non-existing-host-name", func(t *testing.T) { | ||
currentTime := currentServerTime("non-existing-host-name", 60) | ||
require.Zerof(t, currentTime, "server time for non-existing time is expected to be zero, but was %d instead", currentTime) | ||
}) | ||
|
||
t.Run("server-behind", func(t *testing.T) { | ||
serverTimeMapMutex.Lock() | ||
ServerTimeMap["TestCurrentServerTime-server-behind"] = ServerTimeRecord{ServerTime: 27, LocalTimeRecorded: 23} | ||
serverTimeMapMutex.Unlock() | ||
|
||
currentTime := currentServerTime("TestCurrentServerTime-server-behind", 500) | ||
defer func() { | ||
serverTimeMapMutex.Lock() | ||
delete(ServerTimeMap, "TestCurrentServerTime-server-behind") | ||
serverTimeMapMutex.Unlock() | ||
}() | ||
|
||
require.Zerof(t, currentTime, "server time is too old and the method should have returned 0; instead, %d was returned", currentTime) | ||
}) | ||
|
||
t.Run("normal", func(t *testing.T) { | ||
serverTimeMapMutex.Lock() | ||
ServerTimeMap["TestCurrentServerTime-server"] = ServerTimeRecord{ServerTime: 27, LocalTimeRecorded: 23} | ||
serverTimeMapMutex.Unlock() | ||
|
||
defer func() { | ||
serverTimeMapMutex.Lock() | ||
delete(ServerTimeMap, "TestCurrentServerTime-server") | ||
serverTimeMapMutex.Unlock() | ||
}() | ||
|
||
currentTime := currentServerTime("TestCurrentServerTime-server", 37) | ||
require.Equalf(t, currentTime, int64(41), "currentServerTime should have returned %d, but returned %d instead", 41, currentTime) | ||
}) | ||
} |
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