summaryrefslogtreecommitdiffstats
path: root/val_test.go
blob: 188f8a8121373eec3d5098ee6cf4c9ddd8cfbd87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)
	}
}