summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-08-22 21:18:57 -0400
committerSam Anthony <sam@samanthony.xyz>2024-08-22 21:18:57 -0400
commit67e43f976e2e07252b786e584e0290a516630df6 (patch)
treeec68f96aa6247280b395d0949c08bbc684d00826
parent4f0f92e1a7fc67ec2521e2acb5ba7c60f0d6dcc6 (diff)
downloadshare-67e43f976e2e07252b786e584e0290a516630df6.zip
test Val
-rw-r--r--slice_test.go6
-rw-r--r--val_test.go42
2 files changed, 45 insertions, 3 deletions
diff --git a/slice_test.go b/slice_test.go
index f8a1d0b..14fcd10 100644
--- a/slice_test.go
+++ b/slice_test.go
@@ -9,14 +9,14 @@ import (
func TestConstSlice(t *testing.T) {
orig := []string{"foo", "bar", "baz"}
shared := share.NewConstSlice(orig)
- verifySame(shared, orig, t)
+ verifySameSlice(shared, orig, t)
go func() {
defer shared.Close()
- verifySame(shared, orig, t)
+ verifySameSlice(shared, orig, t)
}()
}
-func verifySame[T comparable](cs share.ConstSlice[T], s []T, t *testing.T) {
+func verifySameSlice[T comparable](cs share.ConstSlice[T], s []T, t *testing.T) {
i := 0
for elem := range cs.Elems() {
if i < len(s) {
diff --git a/val_test.go b/val_test.go
new file mode 100644
index 0000000..188f8a8
--- /dev/null
+++ b/val_test.go
@@ -0,0 +1,42 @@
+package share_test
+
+import (
+ "testing"
+
+ "git.samanthony.xyz/share"
+)
+
+// Set value in local goroutine, verify in remote goroutine.
+func TestValSetLocal(t *testing.T) {
+ val := "foo"
+ sharedVal := share.NewVal[string]()
+ sharedVal.Set <- val
+ verifySameVal(sharedVal, val, t)
+ go func() {
+ defer sharedVal.Close()
+ verifySameVal(sharedVal, val, t)
+ }()
+}
+
+// Set value in remote goroutine, verify in local goroutine.
+func TestValSetRemote(t *testing.T) {
+ val := "foo"
+ sharedVal := share.NewVal[string]()
+ defer sharedVal.Close()
+ done := make(chan bool)
+ defer close(done)
+ go func() {
+ sharedVal.Set <- val
+ verifySameVal(sharedVal, val, t)
+ done <- true
+ }()
+ verifySameVal(sharedVal, val, t)
+ <-done
+}
+
+func verifySameVal[T comparable](sv share.Val[T], v T, t *testing.T) {
+ ret := sv.Get()
+ if ret != v {
+ t.Errorf("Val.Get() = %v; expected %v", ret, v)
+ }
+}