File: channellib_test.go

package info (click to toggle)
golang-github-yuin-gopher-lua 0.0~git20170915.0.eb1c729-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 852 kB
  • sloc: yacc: 479; python: 73; makefile: 18
file content (261 lines) | stat: -rw-r--r-- 6,023 bytes parent folder | download | duplicates (2)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package lua

import (
	"reflect"
	"sync"
	"testing"
	"time"
)

func TestChannelMake(t *testing.T) {
	L := NewState()
	defer L.Close()
	errorIfScriptFail(t, L, `
    ch = channel.make()
    `)
	obj := L.GetGlobal("ch")
	ch, ok := obj.(LChannel)
	errorIfFalse(t, ok, "channel expected")
	errorIfNotEqual(t, 0, reflect.ValueOf(ch).Cap())
	close(ch)

	errorIfScriptFail(t, L, `
    ch = channel.make(10)
    `)
	obj = L.GetGlobal("ch")
	ch, _ = obj.(LChannel)
	errorIfNotEqual(t, 10, reflect.ValueOf(ch).Cap())
	close(ch)
}

func TestChannelSelectError(t *testing.T) {
	L := NewState()
	defer L.Close()
	errorIfScriptFail(t, L, `ch = channel.make()`)
	errorIfScriptNotFail(t, L, `channel.select({1,2,3})`, "invalid select case")
	errorIfScriptNotFail(t, L, `channel.select({"<-|", 1, 3})`, "invalid select case")
	errorIfScriptNotFail(t, L, `channel.select({"<-|", ch, function() end})`, "can not send a function")
	errorIfScriptNotFail(t, L, `channel.select({"|<-", 1, 3})`, "invalid select case")
	errorIfScriptNotFail(t, L, `channel.select({"<-->", 1, 3})`, "invalid channel direction")
	errorIfScriptFail(t, L, `ch:close()`)
}

func TestChannelSelect1(t *testing.T) {
	var result LValue
	var wg sync.WaitGroup
	receiver := func(ch, quit chan LValue) {
		defer wg.Done()
		L := NewState()
		defer L.Close()
		L.SetGlobal("ch", LChannel(ch))
		L.SetGlobal("quit", LChannel(quit))
		if err := L.DoString(`
    buf = ""
    local exit = false
    while not exit do
      channel.select(
        {"|<-", ch, function(ok, v)
          if not ok then
            buf = buf .. "channel closed"
            exit = true
          else
            buf = buf .. "received:" .. v
          end
        end},
        {"|<-", quit, function(ok, v)
            buf = buf .. "quit"
        end}
      )
    end
  `); err != nil {
			panic(err)
		}
		result = L.GetGlobal("buf")
	}

	sender := func(ch, quit chan LValue) {
		defer wg.Done()
		L := NewState()
		defer L.Close()
		L.SetGlobal("ch", LChannel(ch))
		L.SetGlobal("quit", LChannel(quit))
		if err := L.DoString(`
    ch:send("1")
    ch:send("2")
  `); err != nil {
			panic(err)
		}
		ch <- LString("3")
		quit <- LTrue
		time.Sleep(1 * time.Second)
		close(ch)
	}

	ch := make(chan LValue)
	quit := make(chan LValue)
	wg.Add(2)
	go receiver(ch, quit)
	go sender(ch, quit)
	wg.Wait()
	lstr, ok := result.(LString)
	errorIfFalse(t, ok, "must be string")
	str := string(lstr)
	errorIfNotEqual(t, "received:1received:2received:3quitchannel closed", str)

}

func TestChannelSelect2(t *testing.T) {
	var wg sync.WaitGroup
	receiver := func(ch, quit chan LValue) {
		defer wg.Done()
		L := NewState()
		defer L.Close()
		L.SetGlobal("ch", LChannel(ch))
		L.SetGlobal("quit", LChannel(quit))
		errorIfScriptFail(t, L, `
           idx, rcv, ok = channel.select(
               {"|<-", ch},
               {"|<-", quit}
           )
           assert(idx == 1)
           assert(rcv == "1")
           assert(ok)
           idx, rcv, ok = channel.select(
               {"|<-", ch},
               {"|<-", quit}
           )
           assert(idx == 1)
           assert(rcv == nil)
           assert(not ok)
       `)
	}

	sender := func(ch, quit chan LValue) {
		defer wg.Done()
		L := NewState()
		defer L.Close()
		L.SetGlobal("ch", LChannel(ch))
		L.SetGlobal("quit", LChannel(quit))
		errorIfScriptFail(t, L, `ch:send("1")`)
		errorIfScriptFail(t, L, `ch:close()`)
	}

	ch := make(chan LValue)
	quit := make(chan LValue)
	wg.Add(2)
	go receiver(ch, quit)
	go sender(ch, quit)
	wg.Wait()
}

func TestChannelSelect3(t *testing.T) {
	var wg sync.WaitGroup
	receiver := func(ch chan LValue) {
		defer wg.Done()
		L := NewState()
		defer L.Close()
		L.SetGlobal("ch", LChannel(ch))
		errorIfScriptFail(t, L, `
           ok = true
           while ok do
             idx, rcv, ok = channel.select(
                 {"|<-", ch}
             )
           end
       `)
	}

	sender := func(ch chan LValue) {
		defer wg.Done()
		L := NewState()
		defer L.Close()
		L.SetGlobal("ch", LChannel(ch))
		errorIfScriptFail(t, L, `
           ok = false
           channel.select(
               {"<-|", ch, "1", function(v)
                 ok = true
               end}
           )
           assert(ok)
           idx, rcv, ok = channel.select(
               {"<-|", ch, "1"}
           )
           assert(idx == 1)
           ch:close()
       `)
	}

	ch := make(chan LValue)
	wg.Add(2)
	go receiver(ch)
	time.Sleep(1)
	go sender(ch)
	wg.Wait()
}

func TestChannelSelect4(t *testing.T) {
	var wg sync.WaitGroup
	receiver := func(ch chan LValue) {
		defer wg.Done()
		L := NewState()
		defer L.Close()
		L.SetGlobal("ch", LChannel(ch))
		errorIfScriptFail(t, L, `
           idx, rcv, ok = channel.select(
                 {"|<-", ch},
                 {"default"}
           )
           assert(idx == 2)
           called = false
           idx, rcv, ok = channel.select(
                 {"|<-", ch},
                 {"default", function()
                    called = true
                 end}
           )
           assert(called)
           ch:close()
       `)
	}

	ch := make(chan LValue)
	wg.Add(1)
	go receiver(ch)
	wg.Wait()
}

func TestChannelSendReceive1(t *testing.T) {
	var wg sync.WaitGroup
	receiver := func(ch chan LValue) {
		defer wg.Done()
		L := NewState()
		defer L.Close()
		L.SetGlobal("ch", LChannel(ch))
		errorIfScriptFail(t, L, `
          local ok, v = ch:receive()
          assert(ok)
          assert(v == "1")
        `)
		time.Sleep(1 * time.Second)
		errorIfScriptFail(t, L, `
          local ok, v = ch:receive()
          assert(not ok)
          assert(v == nil)
        `)
	}
	sender := func(ch chan LValue) {
		defer wg.Done()
		L := NewState()
		defer L.Close()
		L.SetGlobal("ch", LChannel(ch))
		errorIfScriptFail(t, L, `ch:send("1")`)
		errorIfScriptNotFail(t, L, `ch:send(function() end)`, "can not send a function")
		errorIfScriptFail(t, L, `ch:close()`)
	}
	ch := make(chan LValue)
	wg.Add(2)
	go receiver(ch)
	go sender(ch)
	wg.Wait()
}