-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchannelify_test.go
57 lines (46 loc) · 1.03 KB
/
channelify_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
package channelify
import (
"reflect"
"testing"
"time"
)
func TestChannelify_ShouldReturnCorrectType(t *testing.T) {
fn := func() string {
return "hello"
}
typeOfResutnFunc := reflect.TypeOf(func() chan string {
return make(chan string)
})
ch1 := Channelify(fn)
if reflect.TypeOf(ch1) != typeOfResutnFunc {
t.Fail()
}
}
func TestChannelify_ShouldReturnCorrectData(t *testing.T) {
fn := func() string {
time.Sleep(time.Second * 3)
return "hello"
}
ch1 := Channelify(fn)
chV1 := ch1.(func() chan string)()
v1 := <-chV1
if v1 != "hello" {
t.Fail()
}
}
func TestChannelify_ShouldRunInParallel(t *testing.T) {
fn := func() string {
time.Sleep(time.Second * 3)
return "hello"
}
start := time.Now().UnixNano() / int64(time.Millisecond)
ch1 := Channelify(fn)
ch2 := Channelify(fn)
chV1 := ch1.(func() chan string)()
chV2 := ch2.(func() chan string)()
v1, v2 := <-chV1, <-chV2
end := time.Now().UnixNano() / int64(time.Millisecond)
if v1 != "hello" || v2 != "hello" || (end-start) > 4000 {
t.Fail()
}
}