-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine_test.go
97 lines (81 loc) · 2.01 KB
/
combine_test.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright (c) 2024 Mikhail Knyazhev <markus621@yandex.ru>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package xc
import (
"context"
"fmt"
"testing"
"time"
)
func TestUnit_Join(t *testing.T) {
c, cancel := Join(context.Background(), context.Background())
if c == nil {
t.Fatalf("xc.Join returned nil")
}
select {
case <-c.Done():
t.Fatalf("<-c.Done() == it should block")
default:
}
if _, ok := c.Deadline(); ok {
t.Fatalf("c.Deadline() == should not has deadline")
}
cancel()
<-time.After(time.Second)
select {
case <-c.Done():
default:
t.Fatalf("<-c.Done() it shouldn't block")
}
if got, want := fmt.Sprint(c), "xc.Join"; got != want {
t.Fatalf("xc.Join() = %q want %q", got, want)
}
}
func TestUnit_JoinTimeout(t *testing.T) {
ct, _ := context.WithTimeout(context.Background(), 1*time.Second) //nolint: govet
c, _ := Join(context.Background(), context.Background(), ct) //nolint: govet
if c == nil {
t.Fatalf("xc.Join returned nil")
}
select {
case <-c.Done():
t.Fatalf("<-c.Done() == it should block")
default:
}
if _, ok := c.Deadline(); !ok {
t.Fatalf("c.Deadline() == should has deadline")
}
<-c.Done()
select {
case <-c.Done():
default:
t.Fatalf("<-c.Done() it shouldn't block")
}
if got, want := fmt.Sprint(c), "xc.Join"; got != want {
t.Fatalf("xc.Join() = %q want %q", got, want)
}
}
func TestUnit_JoinValue(t *testing.T) {
c, cncl := Join(
SetValue(context.TODO(), "a", 123),
SetValue(context.TODO(), "b", 321),
)
defer cncl()
if c == nil {
t.Fatalf("xc.Join returned nil")
}
if v, ok := GetValue[int](c, "a"); !ok || v != 123 {
t.Fatalf("<-c.Value() should have value")
}
if v, ok := GetValue[int](c, "b"); !ok || v != 321 {
t.Fatalf("<-c.Value() should have value")
}
if _, ok := GetValue[int](c, "c"); ok {
t.Fatalf("<-c.Value() should have not value")
}
if _, ok := GetValue[string](c, "a"); ok {
t.Fatalf("<-c.Value() should have not value")
}
}