53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package orbit
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
)
|
|
|
|
func TestWorkerConcurrencyKeyWireContract(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
support bool
|
|
key string
|
|
token uint64
|
|
valid bool
|
|
}{
|
|
{"legacy", false, "", 0, true},
|
|
{"new_unkeyed", true, "", 0, true},
|
|
{"large_counter", true, "object/资源", ^uint64(0) - 1, true},
|
|
{"not_negotiated", false, "object/a", 2, false},
|
|
{"missing_counter", true, "object/a", 0, false},
|
|
{"unexpected_counter", true, "", 2, false},
|
|
{"leading_space", true, " object/a", 2, false},
|
|
{"control", true, "object/\n", 2, false},
|
|
{"too_long", true, strings.Repeat("字", 192), 2, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
r := workerTestRegistration(t)
|
|
r.ConcurrencyKeys = tc.support
|
|
a := workerTestAssignment(r)
|
|
a.Task.ConcurrencyKey, a.ConcurrencyToken = tc.key, tc.token
|
|
_, w := workerMock(t, r, func(ctx context.Context, c *websocket.Conn) {
|
|
workerTestWrite(t, ctx, c, workerFrame{Type: "task.assign", Body: workerTestBody(t, a)})
|
|
<-ctx.Done()
|
|
})
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
got, err := w.NextAssignment(ctx)
|
|
if tc.valid {
|
|
if err != nil || got.ConcurrencyToken != tc.token || got.Task.ConcurrencyKey != tc.key {
|
|
t.Fatalf("key/token did not survive wire: %v", err)
|
|
}
|
|
} else if !errors.Is(err, ErrWorkerProtocol) {
|
|
t.Fatalf("invalid assignment accepted: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|