-
Notifications
You must be signed in to change notification settings - Fork 6
/
nodejs_test.go
712 lines (564 loc) · 16.5 KB
/
nodejs_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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
// SPDX-FileCopyrightText: 2021 Henry Bubert
//
// SPDX-License-Identifier: MIT
//go:build interop_nodejs
// +build interop_nodejs
package muxrpc
import (
"context"
"encoding/json"
stderr "errors"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"time"
"github.com/pkg/errors"
"github.com/ssbc/go-luigi"
"github.com/stretchr/testify/require"
"go.mindeco.de/log"
"go.mindeco.de/log/level"
"go.mindeco.de/proc"
"github.com/ssbc/go-muxrpc/v2/debug"
)
// This wrapper supplies the manifest for the javascript side
type jsManifestWrapper struct {
root Handler
}
func (w jsManifestWrapper) Handled(m Method) bool { return true }
func (w jsManifestWrapper) HandleConnect(ctx context.Context, edp Endpoint) {
w.root.HandleConnect(ctx, edp)
}
func (w jsManifestWrapper) HandleCall(ctx context.Context, req *Request) {
if req.Method[0] == "manifest" {
err := req.Return(ctx, json.RawMessage(`{
"finalCall": "async",
"version": "sync",
"hello": "async",
"callme": {
"async": "async",
"source": "async",
"magic": "async",
"withAbort": "async"
},
"object": "async",
"stuff": "source",
"magic": "duplex",
"takeSome": "source"
}`))
if err != nil {
fmt.Println("manifest return error:", err)
}
return
}
w.root.HandleCall(ctx, req)
}
func TestJSGettingCalledSource(t *testing.T) {
r := require.New(t)
serv, err := proc.StartStdioProcess("node", os.Stderr, "nodejs_test.js")
r.NoError(err, "nodejs startup")
gotCall := make(chan struct{})
callServed := make(chan struct{})
errc := make(chan error)
ckFatal := mkCheck(errc)
var fh FakeHandler
fh.HandledCalls(func(m Method) bool {
return len(m) == 1 && m[0] == "stuff"
})
fh.HandleCallCalls(func(ctx context.Context, req *Request) {
t.Logf("got call: %+v", req)
close(gotCall)
if req.Type != "source" {
ckFatal(fmt.Errorf("request type: %s", req.Type))
}
binSink, err := req.ResponseSink()
if err != nil {
ckFatal(fmt.Errorf("expected to get sink for replies: %w", err))
}
binSink.SetEncoding(TypeJSON)
enc := json.NewEncoder(binSink)
for i := 0; i < 25; i++ {
var v = struct {
A int `json:"a"`
}{i}
err = enc.Encode(v)
ckFatal(errors.Wrapf(err, "stream pour(%d) failed", i))
}
err = binSink.Close()
if err != nil {
ckFatal(fmt.Errorf("stream close failed: %w", err))
}
close(callServed)
})
muxdbgPath := filepath.Join("testrun", t.Name())
os.RemoveAll(muxdbgPath)
os.MkdirAll(muxdbgPath, 0700)
packer := NewPacker(debug.Dump(muxdbgPath, serv))
handler := jsManifestWrapper{root: &fh}
rpc1 := Handle(packer, handler)
ctx := context.Background()
go serve(ctx, rpc1.(Server), errc)
var v string
err = rpc1.Async(ctx, &v, TypeString, Method{"callme", "source"}, 25)
r.NoError(err, "rcp Async call")
r.Equal("call done", v, "expected call result")
r.Equal(1, fh.HandleConnectCallCount(), "peer did not call 'connect'")
for gotCall != nil || callServed != nil {
select {
case <-gotCall:
t.Log("gotCall closed")
gotCall = nil
case <-callServed:
t.Log("callServed closed")
callServed = nil
}
}
// Already closed?
// r.NoError(packer.Close())
}
func TestJSGettingCalledAsync(t *testing.T) {
r := require.New(t)
errc := make(chan error)
ckFatal := mkCheck(errc)
done := make(chan struct{})
go func() {
<-done
close(errc)
}()
serv, err := proc.StartStdioProcess("node", os.Stderr, "nodejs_test.js")
r.NoError(err, "nodejs startup")
var fh FakeHandler
fh.HandledCalls(func(m Method) bool {
return len(m) == 1 && m[0] == "hello"
})
fh.HandleCallCalls(func(ctx context.Context, req *Request) {
t.Logf("got call: %+v", req)
err := req.Return(ctx, "meow")
ckFatal(err)
})
muxdbgPath := filepath.Join("testrun", t.Name())
os.RemoveAll(muxdbgPath)
os.MkdirAll(muxdbgPath, 0700)
packer := NewPacker(debug.Dump(muxdbgPath, serv))
handler := jsManifestWrapper{root: &fh}
rpc1 := Handle(packer, handler)
ctx := context.Background()
go serve(ctx, rpc1.(Server), errc, done)
var v string
err = rpc1.Async(ctx, &v, TypeString, Method{"callme", "async"})
r.NoError(err, "rcp Async call")
r.Equal(v, "call done", "expected call result")
err = rpc1.Async(ctx, &v, TypeString, Method{"finalCall"}, 1000)
r.NoError(err, "rcp shutdown call")
r.Equal(v, "ty", "expected call result")
for err := range errc {
if err != nil {
t.Fatal(err)
}
}
r.Equal(1, fh.HandleConnectCallCount(), "peer did not call 'connect'")
r.Equal(1, fh.HandleCallCallCount(), "peer did not call")
// r.NoError(packer.Close())
}
/*see that we can do sync as async calls
this feature is just usefull in JSland
i.e.
ver = sbot.version()
vs
sbot.whoami((err, who) => {
who..
})
*/
func TestJSSyncString(t *testing.T) {
r := require.New(t)
serv, err := proc.StartStdioProcess("node", os.Stderr, "nodejs_test.js")
r.NoError(err, "nodejs startup")
muxdbgPath := filepath.Join("testrun", t.Name())
os.RemoveAll(muxdbgPath)
os.MkdirAll(muxdbgPath, 0700)
packer := NewPacker(debug.Dump(muxdbgPath, serv))
var fh FakeHandler
handler := jsManifestWrapper{root: &fh}
rpc1 := Handle(packer, handler)
ctx := context.Background()
errc := make(chan error)
done := make(chan struct{})
go serve(ctx, rpc1.(Server), errc, done)
var v1 string
err = rpc1.Async(ctx, &v1, TypeString, Method{"version"}, "some", "params", 23)
r.NoError(err, "rcp sync call")
r.Equal("some/[email protected]", v1, "expected call result")
var v2 string
err = rpc1.Async(ctx, &v2, TypeString, Method{"version"}, "wrong", "params", 42)
r.Error(err, "rcp sync call")
r.Equal("", v2, "unexpected call result")
var v3 string
err = rpc1.Async(ctx, &v3, TypeString, Method{"finalCall"}, 2000)
r.NoError(err, "rcp shutdown call")
r.Equal("ty", v3, "expected call result")
r.Equal(1, fh.HandleConnectCallCount(), "peer did not call 'connect'")
r.Equal(0, fh.HandleCallCallCount(), "peer did call unexpectedly")
r.NoError(packer.Close())
go func() {
<-done
close(errc)
}()
for err := range errc {
if err != nil {
t.Fatal(err)
}
}
}
func TestJSAsyncString(t *testing.T) {
r := require.New(t)
serv, err := proc.StartStdioProcess("node", os.Stderr, "nodejs_test.js")
r.NoError(err, "nodejs startup")
muxdbgPath := filepath.Join("testrun", t.Name())
os.RemoveAll(muxdbgPath)
os.MkdirAll(muxdbgPath, 0700)
packer := NewPacker(debug.Dump(muxdbgPath, serv))
var fh FakeHandler
handler := jsManifestWrapper{root: &fh}
rpc1 := Handle(packer, handler)
errc := make(chan error)
done := make(chan struct{})
go func() {
<-done
close(errc)
}()
ctx := context.Background()
go serve(ctx, rpc1.(Server), errc, done)
var v string
err = rpc1.Async(ctx, &v, TypeString, Method{"hello"}, "world", "bob")
r.NoError(err, "rcp Async call")
r.Equal("hello, world and bob!", v, "expected call result")
err = rpc1.Async(ctx, &v, TypeString, Method{"finalCall"}, 1000)
r.NoError(err, "rcp shutdown call")
r.Equal(v, "ty", "expected call result")
for err := range errc {
if err != nil {
t.Fatal(err)
}
}
r.Equal(1, fh.HandleConnectCallCount(), "peer did not call 'connect'")
r.Equal(0, fh.HandleCallCallCount(), "peer did call unexpectedly")
// r.NoError(packer.Close())
}
func TestJSAsyncObject(t *testing.T) {
r := require.New(t)
serv, err := proc.StartStdioProcess("node", os.Stderr, "nodejs_test.js")
r.NoError(err, "nodejs startup")
muxdbgPath := filepath.Join("testrun", t.Name())
os.RemoveAll(muxdbgPath)
os.MkdirAll(muxdbgPath, 0700)
packer := NewPacker(debug.Dump(muxdbgPath, serv))
var fh FakeHandler
handler := jsManifestWrapper{root: &fh}
rpc1 := Handle(packer, handler)
ctx := context.Background()
errc := make(chan error)
done := make(chan struct{})
go func() {
<-done
close(errc)
}()
go serve(ctx, rpc1.(Server), errc, done)
var resp struct {
With string `json:"with"`
}
err = rpc1.Async(ctx, &resp, TypeJSON, Method{"object"})
r.NoError(err, "rcp Async call")
r.Equal("fields!", resp.With, "wrong call response")
var str string
err = rpc1.Async(ctx, &str, TypeString, Method{"finalCall"}, 1000)
r.NoError(err, "rcp shutdown call")
r.Equal("ty", str, "expected call result")
for err := range errc {
if err != nil {
t.Fatal(err)
}
}
r.Equal(1, fh.HandleConnectCallCount(), "peer did not call 'connect'")
r.Equal(0, fh.HandleCallCallCount(), "peer did call unexpectedly")
// r.NoError(packer.Close())
}
func TestJSSource(t *testing.T) {
r := require.New(t)
serv, err := proc.StartStdioProcess("node", os.Stderr, "nodejs_test.js")
r.NoError(err, "nodejs startup")
muxdbgPath := filepath.Join("testrun", t.Name())
os.RemoveAll(muxdbgPath)
os.MkdirAll(muxdbgPath, 0700)
packer := NewPacker(debug.Dump(muxdbgPath, serv))
var fh FakeHandler
handler := jsManifestWrapper{root: &fh}
rpc1 := Handle(packer, handler)
ctx, cancel := context.WithCancel(context.Background())
errc := make(chan error)
done := make(chan struct{})
go func() {
<-done
close(errc)
}()
go serve(ctx, rpc1.(Server), errc, done)
type obj struct {
A int
}
src, err := rpc1.Source(ctx, TypeJSON, Method{"stuff"})
r.NoError(err, "rcp Async call")
for i := 1; i < 5; i++ {
more := src.Next(ctx)
r.True(more, "src.Next %d", i)
var v obj
err := src.Reader(func(r io.Reader) error {
return json.NewDecoder(r).Decode(&v)
})
r.NoError(err, "decode: %d", i)
r.Equal(i, v.A, "result value: %d", i)
}
more := src.Next(ctx)
r.False(more, "src.Next no more")
r.NoError(src.Err())
var str string
err = rpc1.Async(ctx, &str, TypeString, Method{"finalCall"}, 1000)
r.NoError(err, "rcp shutdown call")
r.Equal("ty", str, "expected call result")
cancel()
// r.NoErrorf(packer.Close(), "%+s %s", "error closing packer")
for err := range errc {
if err != nil {
t.Error(err)
}
}
r.Equal(1, fh.HandleConnectCallCount(), "peer did not call 'connect'")
r.Equal(0, fh.HandleCallCallCount(), "peer did call unexpectedly")
}
func TestJSDuplex(t *testing.T) {
r := require.New(t)
serv, err := proc.StartStdioProcess("node", os.Stderr, "nodejs_test.js")
r.NoError(err, "nodejs startup")
muxdbgPath := filepath.Join("testrun", t.Name())
os.RemoveAll(muxdbgPath)
os.MkdirAll(muxdbgPath, 0700)
packer := NewPacker(debug.Dump(muxdbgPath, serv))
var fh FakeHandler
handler := jsManifestWrapper{root: &fh}
rpc1 := Handle(packer, handler)
ctx := context.Background()
errc := make(chan error)
done := make(chan struct{})
go func() {
<-done
close(errc)
}()
go serve(ctx, rpc1.(Server), errc, done)
src, snk, err := rpc1.Duplex(ctx, TypeJSON, Method{"magic"})
r.NoError(err, "rcp Async call")
fmt.Println("command started")
i := 0
var str = []string{"a", "b", "c", "d", "e"}
send := luigi.FuncSource(func(_ context.Context) (interface{}, error) {
defer func() { i++ }()
if i < len(str) {
// fmt.Println("to snk", str[i])
time.Sleep(time.Second * 1)
return str[i], nil
}
r.NoError(snk.Close())
return nil, luigi.EOS{}
})
enc := json.NewEncoder(snk)
luigiSnk := luigi.FuncSink(func(_ context.Context, v interface{}, err error) error {
if err != nil {
if luigi.IsEOS(err) {
return err
}
}
err = enc.Encode(v)
if err != nil {
return err
}
return err
})
r.NoError(luigi.Pump(ctx, luigiSnk, send))
fmt.Println("filled sink")
print := luigi.FuncSink(func(_ context.Context, v interface{}, err error) error {
fmt.Println("from src:", v, err)
return err
})
r.NoError(luigi.Pump(ctx, print, src.AsStream()))
fmt.Println("draind src")
// r.NoError(packer.Close())
// close(errc)
// for err := range errc {
// if err != nil {
// t.Fatal(err)
// }
// }
}
func TestJSDuplexToUs(t *testing.T) {
r := require.New(t)
jsLog := log.NewLogfmtLogger(os.Stderr)
serv, err := proc.StartStdioProcess("node", os.Stderr, "nodejs_test.js")
r.NoError(err, "nodejs startup")
var h hDuplex
h.encoding = TypeJSON
h.failed = make(chan error)
muxdbg := log.With(jsLog, "u", "pkts")
h.logger = muxdbg
h.txvals = []interface{}{"a", "b", "c", "d", "e", struct{ RXJS int }{9}}
muxdbgPath := filepath.Join("testrun", t.Name())
os.RemoveAll(muxdbgPath)
os.MkdirAll(muxdbgPath, 0700)
packer := NewPacker(debug.Dump(muxdbgPath, serv))
mh := jsManifestWrapper{root: &h}
rpc1 := Handle(packer, mh)
ctx, cancel := context.WithCancel(context.Background())
errc := make(chan error)
go serve(ctx, rpc1.(Server), errc)
var ret string
err = rpc1.Async(ctx, &ret, TypeString, Method{"callme", "magic"})
r.NoError(err, "nodejs startup")
r.EqualValues("yey", ret)
r.NoError(<-h.failed)
err = rpc1.Async(ctx, &ret, TypeString, Method{"finalCall"}, 2000)
r.NoError(err, "rcp shutdown call")
r.Equal("ty", ret, "expected call result")
cancel()
rpc1.Terminate()
close(errc)
for err := range errc {
r.NoError(err)
}
}
func TestJSNoSuchMethod(t *testing.T) {
r := require.New(t)
serv, err := proc.StartStdioProcess("node", os.Stderr, "nodejs_test.js")
r.NoError(err, "nodejs startup")
muxdbgPath := filepath.Join("testrun", t.Name())
os.RemoveAll(muxdbgPath)
os.MkdirAll(muxdbgPath, 0700)
packer := NewPacker(debug.Dump(muxdbgPath, serv))
var h FakeHandler
mh := jsManifestWrapper{root: &h}
rpc1 := Handle(packer, mh)
ctx, cancel := context.WithCancel(context.Background())
errc := make(chan error)
go serve(ctx, rpc1.(Server), errc)
// async
var ret string
err = rpc1.Async(ctx, &ret, TypeString, Method{"nosuch", "async"})
r.Error(err, "call executed - async")
r.EqualValues("", ret)
var ce ErrNoSuchMethod
r.True(stderr.As(err, &ce), "wrong error: %s", err)
r.Equal("nosuch.async", ce.Method.String())
// source
src, err := rpc1.Source(ctx, TypeString, Method{"nosuch", "source"})
r.True(stderr.As(err, &ce), "wrong error: %s", err)
r.Equal("nosuch.source", ce.Method.String())
r.Nil(src, "source should be nil")
// TODO: refactor these to rpc1.OnManifest(method) == false
// sink
snk, err := rpc1.Sink(ctx, TypeString, Method{"nosuch", "sink"})
r.Nil(snk)
r.Error(err, "call executed - sink")
r.True(stderr.As(err, &ce), "wrong error: %s", err)
r.Equal("nosuch.sink", ce.Method.String())
// duplex
src, snk, err = rpc1.Duplex(ctx, TypeString, Method{"nosuch", "duplex"})
r.Error(err, "call executed - duplex")
r.Nil(snk)
r.True(stderr.As(err, &ce), "wrong error: %s", err)
r.Equal("nosuch.duplex", ce.Method.String())
// cleanup
err = rpc1.Async(ctx, &ret, TypeString, Method{"finalCall"}, 2000)
r.NoError(err, "rcp shutdown call")
r.Equal("ty", ret, "expected call result")
cancel()
rpc1.Terminate()
close(errc)
for err := range errc {
r.NoError(err)
}
}
func TestJSSupportAbort(t *testing.T) {
ctx := context.Background()
if dl, ok := t.Deadline(); ok {
dl = dl.Add(-10 * time.Second)
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, dl)
defer cancel()
}
r := require.New(t)
jsLog := log.NewLogfmtLogger(os.Stderr)
serv, err := proc.StartStdioProcess("node", os.Stderr, "nodejs_test.js")
r.NoError(err, "nodejs startup")
var h hAbortMe
h.want = 5
h.t = t
h.logger = jsLog
muxdbgPath := filepath.Join("testrun", t.Name())
os.RemoveAll(muxdbgPath)
os.MkdirAll(muxdbgPath, 0700)
packer := NewPacker(debug.Dump(muxdbgPath, serv))
mh := jsManifestWrapper{root: &h}
rpc1 := Handle(packer, mh, WithContext(ctx))
errc := make(chan error)
go serve(ctx, rpc1.(Server), errc)
var ret string
err = rpc1.Async(ctx, &ret, TypeString, Method{"callme", "withAbort"}, h.want)
r.NoError(err, "call failed")
r.EqualValues("thanks!", ret)
t.Log("abort done. shutting down")
var ret2 string
err = rpc1.Async(ctx, &ret2, TypeString, Method{"finalCall"}, 1000)
r.NoError(err, "rcp shutdown call")
r.Equal("ty", ret2, "expected correct call result")
rpc1.Terminate()
close(errc)
for err := range errc {
r.NoError(err)
}
}
type hAbortMe struct {
want int
logger log.Logger
t *testing.T
}
func (h *hAbortMe) Handled(m Method) bool {
return len(m) == 1 && m[0] == "takeSome"
}
func (h *hAbortMe) HandleConnect(ctx context.Context, e Endpoint) {
h.logger.Log("connect:", e.Remote())
}
func (h *hAbortMe) HandleCall(ctx context.Context, req *Request) {
snk, err := req.ResponseSink()
if err != nil {
require.NoError(h.t, err)
req.Stream.CloseWithError(err)
return
}
snk.SetEncoding(TypeJSON)
enc := json.NewEncoder(snk)
var i int
for ; i < h.want+10; i++ {
err := enc.Encode(i)
if err != nil {
h.logger.Log("evt", "failed to pour", "i", i, "err", err)
if errors.Cause(err) == context.Canceled || stderr.Is(err, io.EOF) {
break
}
require.NoError(h.t, err, "should end with canceled or eof")
break
}
h.logger.Log("evt", "sent", "i", i)
time.Sleep(time.Second / 4)
}
if i != h.want {
err := fmt.Errorf("expected %d but sent %d packets", h.want, i)
level.Error(h.logger).Log("evt", "sent too much?", "err", err)
require.NoError(h.t, err)
}
}