| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package protocol
- import (
- "bytes"
- "context"
- "encoding/binary"
- "encoding/json"
- "errors"
- "io"
- "net"
- "strings"
- "sync"
- "testing"
- "time"
- )
- type shortWriter struct{ b bytes.Buffer }
- func (w *shortWriter) Write(b []byte) (int, error) {
- if len(b) > 2 {
- b = b[:2]
- }
- return w.b.Write(b)
- }
- type zeroWriter struct{}
- func (zeroWriter) Write([]byte) (int, error) { return 0, nil }
- type badWriter struct{}
- func (badWriter) Write([]byte) (int, error) { return 0, io.ErrClosedPipe }
- func TestFraming(t *testing.T) {
- w := &shortWriter{}
- req := Request{Version: Version, ID: 5, Method: "hello", Params: json.RawMessage(`{"text":"科学"}`)}
- if e := Write(w, req); e != nil {
- t.Fatal(e)
- }
- var out Request
- if e := Read(&w.b, &out); e != nil || out.Method != "hello" {
- t.Fatalf("roundtrip: %+v %v", out, e)
- }
- if Write(zeroWriter{}, req) != io.ErrShortWrite {
- t.Fatal("zero writer did not fail")
- }
- if Write(badWriter{}, req) != io.ErrClosedPipe {
- t.Fatal("write error lost")
- }
- if Write(io.Discard, map[string]string{"large": strings.Repeat("x", MaxMessage)}) == nil {
- t.Fatal("oversized frame accepted")
- }
- for _, n := range []uint32{0, MaxMessage + 1} {
- var b bytes.Buffer
- _ = binary.Write(&b, binary.BigEndian, n)
- if Read(&b, &out) == nil {
- t.Fatal("invalid length accepted")
- }
- }
- for _, b := range [][]byte{{1}, {0, 0, 0, 2, '{'}, {0, 0, 0, 1, 'x'}} {
- if Read(bytes.NewReader(b), &out) == nil {
- t.Fatal("malformed frame accepted")
- }
- }
- if Write(io.Discard, make(chan int)) == nil {
- t.Fatal("unmarshalable value accepted")
- }
- }
- func TestConcurrentCallsCancellationAndEOF(t *testing.T) {
- a, b := net.Pipe()
- serverDone := make(chan error, 1)
- canceled := make(chan struct{}, 1)
- go func() {
- serverDone <- Serve(context.Background(), a, a, func(ctx context.Context, method string, raw json.RawMessage) (any, error) {
- if method == "wait" {
- <-ctx.Done()
- canceled <- struct{}{}
- return nil, ctx.Err()
- }
- if method == "fail" {
- return nil, errors.New("failure")
- }
- return json.RawMessage(raw), nil
- })
- }()
- c := NewClient(b, b)
- var wg sync.WaitGroup
- for i := 0; i < 12; i++ {
- wg.Add(1)
- go func(n int) {
- defer wg.Done()
- var got int
- if e := c.Call(context.Background(), "echo", n, &got); e != nil || got != n {
- t.Errorf("echo %d: %d %v", n, got, e)
- }
- }(i)
- }
- wg.Wait()
- ctx, stop := context.WithTimeout(context.Background(), 30*time.Millisecond)
- defer stop()
- if e := c.Call(ctx, "wait", nil, nil); !errors.Is(e, context.DeadlineExceeded) {
- t.Fatalf("cancellation: %v", e)
- }
- select {
- case <-canceled:
- case <-time.After(time.Second):
- t.Fatal("worker did not receive cancellation")
- }
- if c.Call(context.Background(), "fail", nil, nil) == nil {
- t.Fatal("remote failure lost")
- }
- c.Close()
- if c.Call(context.Background(), "echo", nil, nil) == nil {
- t.Fatal("closed client accepted call")
- }
- b.Close()
- a.Close()
- select {
- case <-serverDone:
- case <-time.After(time.Second):
- t.Fatal("server did not exit")
- }
- }
- func TestVersionAndUnknownCancellation(t *testing.T) {
- var input bytes.Buffer
- _ = Write(&input, Request{Version: 99, ID: 1})
- _ = Write(&input, Request{Version: Version, ID: 2, Method: "request.cancel", Params: json.RawMessage(`{"id":99}`)})
- var output bytes.Buffer
- if err := Serve(context.Background(), &input, &output, func(context.Context, string, json.RawMessage) (any, error) {
- t.Fatal("invalid envelope dispatched")
- return nil, nil
- }); err != nil {
- t.Fatal(err)
- }
- var first, second Response
- if Read(&output, &first) != nil || first.Error == nil {
- t.Fatal("version failure missing")
- }
- if Read(&output, &second) != nil || second.Error != nil {
- t.Fatal("cancel response missing")
- }
- }
|