1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-10-28 15:02:28 +00:00
Files
go-chasquid-smtp/internal/localrpc/e2e_test.go
T
Alberto Bertogli 8b19760f4e test: Use modern testing features instead of custom helpers
Many of our tests rely on helpers that predate features which are now
available in the standard library.

This patch replaces them with the standard equivalents:

- MustTempDir/RemoveIfOk -> t.ArtifactDir() or t.TempDir() (depending on
  whether it is useful to keep the data on request).
- GetFreePort + waitForServer -> opening the listeners before starting
  the servers. To support this, localrpc.Server gains a Serve method. We
  may re-introduce these in the future if more use cases appear.
- WaitFor and real-time timing checks -> testing/synctest.
- context.Background() -> t.Context().
- Benchmark b.N loops -> b.Loop().
- reflect.DeepEqual -> cmp.Diff, and proto.Equal for protobufs.
2026-10-03 10:34:35 +01:00

115 lines
2.4 KiB
Go

package localrpc
import (
"errors"
"net"
"net/url"
"testing"
"blitiri.com.ar/go/chasquid/internal/trace"
"github.com/google/go-cmp/cmp"
)
func Echo(tr *trace.Trace, input url.Values) (url.Values, error) {
return input, nil
}
func Hola(tr *trace.Trace, input url.Values) (url.Values, error) {
output := url.Values{}
output.Set("greeting", "Hola "+input.Get("name"))
return output, nil
}
var errTest = errors.New("test error")
func HolaErr(tr *trace.Trace, input url.Values) (url.Values, error) {
return nil, errTest
}
type testServer struct {
sock string
*Server
}
func newTestServer(t *testing.T) *testServer {
t.Helper()
tsrv := &testServer{
sock: t.TempDir() + "/sock",
Server: NewServer(),
}
tsrv.Register("Echo", Echo)
tsrv.Register("Hola", Hola)
tsrv.Register("HolaErr", HolaErr)
lis, err := net.Listen("unix", tsrv.sock)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { lis.Close() })
go tsrv.Serve(lis)
return tsrv
}
func mkV(args ...string) url.Values {
v := url.Values{}
for i := 0; i < len(args); i += 2 {
v.Set(args[i], args[i+1])
}
return v
}
func TestEndToEnd(t *testing.T) {
srv := newTestServer(t)
// Run the client.
client := NewClient(srv.sock)
cases := []struct {
method string
input url.Values
output url.Values
err error
}{
{"Echo", nil, mkV(), nil},
{"Echo", mkV("msg", "hola"), mkV("msg", "hola"), nil},
{"Hola", mkV("name", "marola"), mkV("greeting", "Hola marola"), nil},
{"HolaErr", nil, nil, errTest},
{"UnknownMethod", nil, nil, errUnknownMethod},
}
for _, c := range cases {
t.Run(c.method, func(t *testing.T) {
resp, err := client.CallWithValues(c.method, c.input)
if diff := cmp.Diff(c.err, err, transformErrors); diff != "" {
t.Errorf("error mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(c.output, resp); diff != "" {
t.Errorf("output mismatch (-want +got):\n%s", diff)
}
})
}
// Check Call too.
output, err := client.Call("Hola", "name", "marola")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if diff := cmp.Diff(mkV("greeting", "Hola marola"), output); diff != "" {
t.Errorf("output mismatch (-want +got):\n%s", diff)
}
}
// Allow us to compare errors with cmp.Diff by their string content (since the
// instances/types don't carry across RPC boundaries).
var transformErrors = cmp.Transformer(
"error",
func(err error) string {
if err == nil {
return "<nil>"
}
return err.Error()
})