generated from matrix-org/.github
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for some rust client code
- Loading branch information
Showing
5 changed files
with
154 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package rust | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
) | ||
|
||
func mustEqual(t *testing.T, got, want []int, msg string) { | ||
t.Helper() | ||
if !slices.Equal(got, want) { | ||
t.Errorf("%s, got %v want %v", msg, got, want) | ||
} | ||
} | ||
|
||
func TestDynamicSlice(t *testing.T) { | ||
var slice DynamicSlice[int] | ||
slice.Append(5, 6, 7) | ||
mustEqual(t, slice.Slice, []int{5, 6, 7}, "Append") | ||
slice.Insert(0, 42) | ||
mustEqual(t, slice.Slice, []int{42, 5, 6, 7}, "Insert") | ||
slice.Insert(2, 43) | ||
mustEqual(t, slice.Slice, []int{42, 5, 43, 6, 7}, "Insert") | ||
slice.Insert(5, 44) | ||
mustEqual(t, slice.Slice, []int{42, 5, 43, 6, 7, 44}, "Insert") | ||
slice.PopBack() | ||
mustEqual(t, slice.Slice, []int{42, 5, 43, 6, 7}, "PopBack") | ||
slice.PopFront() | ||
mustEqual(t, slice.Slice, []int{5, 43, 6, 7}, "PopFront") | ||
slice.Remove(1) | ||
mustEqual(t, slice.Slice, []int{5, 6, 7}, "Remove") | ||
slice.Set(1, 77) | ||
mustEqual(t, slice.Slice, []int{5, 77, 7}, "Set") | ||
slice.Truncate(2) | ||
mustEqual(t, slice.Slice, []int{5, 77}, "Truncate") | ||
slice.Reset([]int{2, 3, 5, 7, 11}) | ||
mustEqual(t, slice.Slice, []int{2, 3, 5, 7, 11}, "Reset") | ||
slice.Append(13) | ||
mustEqual(t, slice.Slice, []int{2, 3, 5, 7, 11, 13}, "Append") | ||
slice.PushBack(17) | ||
mustEqual(t, slice.Slice, []int{2, 3, 5, 7, 11, 13, 17}, "PushBack") | ||
slice.PushFront(1) | ||
mustEqual(t, slice.Slice, []int{1, 2, 3, 5, 7, 11, 13, 17}, "PushFront") | ||
slice.Clear() | ||
mustEqual(t, slice.Slice, []int{}, "Clear") | ||
} |
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,36 @@ | ||
package rust | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/matrix-org/complement/must" | ||
) | ||
|
||
func receiveFromChannel(t *testing.T, ch <-chan string) string { | ||
t.Helper() | ||
select { | ||
case val := <-ch: | ||
return val | ||
case <-time.After(time.Second): | ||
t.Fatalf("failed to receive from channel") | ||
} | ||
return "" | ||
} | ||
|
||
func TestGenericStateListener(t *testing.T) { | ||
l := newGenericStateListener[string]() | ||
go l.OnUpdate("foo") | ||
must.Equal(t, receiveFromChannel(t, l.ch), "foo", "OnUpdate") | ||
go l.OnUpdate("bar") | ||
must.Equal(t, receiveFromChannel(t, l.ch), "bar", "OnUpdate") | ||
|
||
// can close and then no more updates get sent | ||
l.Close() | ||
l.OnUpdate("baz") // this should not block due to not sending on the channel | ||
must.Equal(t, receiveFromChannel(t, l.ch), "", "Closed") // recv on a closed channel is the zero value | ||
|
||
// can close repeatedly without panicking | ||
l.Close() | ||
l.Close() | ||
} |
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,64 @@ | ||
package rust | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/matrix-org/complement/must" | ||
) | ||
|
||
func TestRoomListener(t *testing.T) { | ||
rl := NewRoomsListener() | ||
|
||
// basic functionality | ||
recv := make(chan string, 2) | ||
cancel := rl.AddListener(func(broadcastRoomID string) (cancel bool) { | ||
recv <- broadcastRoomID | ||
return false | ||
}) | ||
rl.BroadcastUpdateForRoom("foo") | ||
must.Equal(t, <-recv, "foo", "basic usage") | ||
|
||
// multiple broadcasts | ||
rl.BroadcastUpdateForRoom("bar") | ||
rl.BroadcastUpdateForRoom("baz") | ||
must.Equal(t, <-recv, "bar", "multiple broadcasts") | ||
must.Equal(t, <-recv, "baz", "multiple broadcasts") | ||
|
||
// multiple listeners | ||
recv2 := make(chan string, 2) | ||
shouldCancel := false | ||
cancel2 := rl.AddListener(func(broadcastRoomID string) (cancel bool) { | ||
recv2 <- broadcastRoomID | ||
return shouldCancel | ||
}) | ||
rl.BroadcastUpdateForRoom("ping") | ||
must.Equal(t, <-recv, "ping", "multiple listeners") | ||
must.Equal(t, <-recv2, "ping", "multiple listeners") | ||
|
||
// once cancelled, no more data | ||
cancel() | ||
rl.BroadcastUpdateForRoom("quuz") | ||
select { | ||
case <-recv: | ||
t.Fatalf("received room id after cancel()") | ||
default: | ||
// we expect to hit this | ||
} | ||
// but the 2nd listener gets it | ||
must.Equal(t, <-recv2, "quuz", "uncancelled listener") | ||
|
||
// returning true from the listener automatically cancels | ||
shouldCancel = true | ||
rl.BroadcastUpdateForRoom("final message") | ||
must.Equal(t, <-recv2, "final message", "cancel bool") | ||
rl.BroadcastUpdateForRoom("no one is listening") | ||
select { | ||
case <-recv2: | ||
t.Fatalf("received room id after returning true") | ||
default: | ||
// we expect to hit this | ||
} | ||
|
||
// calling the cancel() function in addition to returning true no-ops | ||
cancel2() | ||
} |
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