-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_simple_test.go
60 lines (48 loc) · 1.41 KB
/
01_simple_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
package go_channel__test
import (
"testing"
"time"
)
////////////////////////////////////////////////////////////
// UNBUFFERED CHANNEL
// With unbuffered channel you need a goroutine to write or read channel data
////////////////////////////////////////////////////////////
// A) read c value "string 01"
// B) read c value "string 02"
// finish
func Test_simple_goroutine_send_data(t *testing.T) {
// UNbuffered channel
c := make(chan string)
// goroutine send data in channel
go func() {
c <- "string 01"
// it will block here until first value was read
c <- "string 02"
}()
// sync read first value (it wait until value was send)
t.Logf("A) read c value \"%s\"\n", <-c)
// sync read second value (it wait until value was send)
t.Logf("B) read c value \"%s\"\n", <-c)
t.Log("finish")
}
// A) read c value "string 01"
// B) read c value "string 02"
// finish
func Test_simple_goroutine_read_data(t *testing.T) {
// UNbuffered channel
c := make(chan string)
// goroutine read channel data
go func() {
// sync read first value (it wait until value was send)
t.Logf("A) read c value \"%s\"\n", <-c)
// sync read second value (it wait until value was send)
t.Logf("B) read c value \"%s\"\n", <-c)
}()
c <- "string 01"
// it will block here until first value was read
c <- "string 02"
close(c)
t.Log("finish")
// give time to goroutine to read second value
time.Sleep(100 * time.Millisecond)
}