Files
fun/stream_protocol_test.go
T
chiyi 41df889a1a v2.0.0: error-channel APIs, correctness fixes, and server hardening
BREAKING CHANGES:
- Guard interface: Guard(ctx Ctx) error; returning error short-circuits
  (subsequent guards and the method no longer execute)
- Wired[T]() (*T, error); New() may return error; failed wiring is sticky
- BindService/BindGuard/BindRoute return error; routes accept guards
  (guards receive merged query/form params as Ctx.State)
- registration panics after Start; Ctx.Ip honors X-Forwarded-For/X-Real-IP
- internal errors sanitized to fixed client messages

FIXES:
- int64 precision loss: /cell data decoded from raw JSON bytes and
  responses serialized with json.Number (no float64 round-trip)
- enum values range-checked before uint8 conversion (256 no longer
  truncates to 0 and slips through)
- logger: files failing name parsing are no longer deleted; log channel
  never blocks request goroutines; ConfigLogger is race-free;
  logWriterWorker unlock bug fixed
- stream writer panics recovered (process no longer crashes);
  streamDone closed exactly once
- generated Go client emits definitions for pointer-to-enum/struct fields
- anonymous structs rejected at registration; isPrivate safe on empty names
- removed dead Result.Id and RequestInfo.Type

ADDITIONS:
- graceful shutdown (Shutdown), StartOn, server timeouts by default
  (Read 60s/Idle 120s/Write off), SetTimeouts/SetMaxConcurrency
- big-int-safe JSON in the generated TS client (>2^53 as BigInt)
- example/ demo services and cmd/genexample artifact generator
2026-09-03 15:32:07 +08:00

114 lines
2.9 KiB
Go

package fun
import (
"encoding/json"
"errors"
"io"
"net"
"net/http"
"strings"
"testing"
"github.com/valyala/fasthttp"
)
type ProtocolStreamSvc struct{}
func (*ProtocolStreamSvc) Empty() (*Stream, error) {
stream := &Stream{}
go stream.Close()
return stream, nil
}
func (*ProtocolStreamSvc) Before() (*Stream, error) {
return nil, errors.New("before stream")
}
func (*ProtocolStreamSvc) Business() (*Stream, error) {
return nil, Error(4201, "business before stream")
}
func serveFun(t *testing.T, f *Fun) string {
t.Helper()
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
done := make(chan error, 1)
go func() { done <- fasthttp.Serve(listener, f.handle) }()
t.Cleanup(func() {
_ = listener.Close()
<-done
})
return "http://" + listener.Addr().String()
}
func streamPost(t *testing.T, url, method string) (*http.Response, []byte) {
t.Helper()
body := strings.NewReader(`{"serviceName":"ProtocolStreamSvc","methodName":"` + method + `"}`)
request, err := http.NewRequest(http.MethodPost, url+"/cell", body)
if err != nil {
t.Fatal(err)
}
request.Header.Set("Content-Type", "application/json")
request.Close = true
response, err := http.DefaultClient.Do(request)
if err != nil {
t.Fatal(err)
}
defer response.Body.Close()
data, err := io.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
return response, data
}
func TestServerStreamContentTypeAndEmptyStream(t *testing.T) {
f := New()
if err := f.BindService(&ProtocolStreamSvc{}); err != nil {
t.Fatal(err)
}
response, body := streamPost(t, serveFun(t, f), "Empty")
if got := response.Header.Get("Content-Type"); got != "application/x-ndjson" {
t.Fatalf("Content-Type = %q", got)
}
if len(body) != 0 {
t.Fatalf("empty stream returned %q", body)
}
}
func TestServerStreamSetupErrorsUseResultProtocol(t *testing.T) {
f := New()
if err := f.BindService(&ProtocolStreamSvc{}); err != nil {
t.Fatal(err)
}
url := serveFun(t, f)
for _, test := range []struct {
method string
status uint8
code uint16
msg string
}{
{method: "Before", status: 1, msg: "before stream"},
{method: "Business", status: 2, code: 4201, msg: "business before stream"},
} {
t.Run(test.method, func(t *testing.T) {
response, body := streamPost(t, url, test.method)
if strings.HasPrefix(response.Header.Get("Content-Type"), "application/x-ndjson") {
t.Fatalf("setup error used stream Content-Type: %q", response.Header.Get("Content-Type"))
}
var result Result[any]
if err := json.Unmarshal(body, &result); err != nil {
t.Fatalf("invalid Result body %q: %v", body, err)
}
if result.Status != test.status || result.Msg == nil || *result.Msg != test.msg {
t.Fatalf("unexpected Result: %+v", result)
}
if test.code != 0 && (result.Code == nil || *result.Code != test.code) {
t.Fatalf("code = %v, want %d", result.Code, test.code)
}
})
}
}