-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa5d3bd
commit 03fac30
Showing
10 changed files
with
390 additions
and
66 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
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,58 @@ | ||
package dispute | ||
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestSendMessage(t *testing.T) { | ||
t.Run("successful send", func(t *testing.T) { | ||
ch := make(chan any, 1) | ||
defer close(ch) | ||
|
||
err := sendMessage(ch, "test") | ||
require.NoError(t, err) | ||
}) | ||
t.Run("timeout", func(t *testing.T) { | ||
ch := make(chan any) | ||
defer close(ch) | ||
|
||
err := sendMessage(ch, "test") | ||
require.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestCall(t *testing.T) { | ||
t.Run("successful call", func(t *testing.T) { | ||
receiver := make(chan any) | ||
response := make(chan any) | ||
defer close(receiver) | ||
defer close(response) | ||
|
||
go func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
select { | ||
case <-receiver: | ||
response <- "pong" | ||
case <-ctx.Done(): | ||
require.NoError(t, ctx.Err()) | ||
} | ||
}() | ||
|
||
res, err := call(receiver, "ping", response) | ||
require.NoError(t, err) | ||
require.Equal(t, "pong", res) | ||
}) | ||
t.Run("timeout", func(t *testing.T) { | ||
receiver := make(chan any) | ||
response := make(chan any) | ||
defer close(receiver) | ||
defer close(response) | ||
|
||
res, err := call(receiver, "ping", response) | ||
require.Error(t, err) | ||
require.Nil(t, res) | ||
}) | ||
} |
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,50 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestGetByzantineThreshold(t *testing.T) { | ||
cases := []struct { | ||
n, expected int | ||
}{ | ||
{0, 0}, | ||
{1, 0}, | ||
{2, 0}, | ||
{3, 0}, | ||
{4, 1}, | ||
{5, 1}, | ||
{6, 1}, | ||
{9, 2}, | ||
{10, 3}, | ||
// Additional cases can be added here | ||
} | ||
|
||
for _, c := range cases { | ||
got := GetByzantineThreshold(c.n) | ||
require.Equal(t, c.expected, got) | ||
} | ||
} | ||
|
||
func TestGetSuperMajorityThreshold(t *testing.T) { | ||
cases := []struct { | ||
n, expected int | ||
}{ | ||
{0, 0}, | ||
{1, 1}, | ||
{2, 2}, | ||
{3, 3}, | ||
{4, 3}, | ||
{5, 4}, | ||
{6, 5}, | ||
{9, 7}, | ||
{10, 7}, | ||
// Additional cases can be added here | ||
} | ||
|
||
for _, c := range cases { | ||
got := GetSuperMajorityThreshold(c.n) | ||
require.Equal(t, c.expected, got) | ||
} | ||
} |
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
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
Oops, something went wrong.