mirror of
https://blitiri.com.ar/repos/chasquid
synced 2026-10-29 15:02:30 +00:00
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.
This commit is contained in:
@@ -4,9 +4,7 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"blitiri.com.ar/go/chasquid/internal/trace"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
@@ -29,7 +27,6 @@ func HolaErr(tr *trace.Trace, input url.Values) (url.Values, error) {
|
||||
}
|
||||
|
||||
type testServer struct {
|
||||
dir string
|
||||
sock string
|
||||
*Server
|
||||
}
|
||||
@@ -37,31 +34,25 @@ type testServer struct {
|
||||
func newTestServer(t *testing.T) *testServer {
|
||||
t.Helper()
|
||||
|
||||
tmpDir, err := os.MkdirTemp("", "rpc-test-*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tsrv := &testServer{
|
||||
dir: tmpDir,
|
||||
sock: tmpDir + "/sock",
|
||||
sock: t.TempDir() + "/sock",
|
||||
Server: NewServer(),
|
||||
}
|
||||
|
||||
tsrv.Register("Echo", Echo)
|
||||
tsrv.Register("Hola", Hola)
|
||||
tsrv.Register("HolaErr", HolaErr)
|
||||
go tsrv.ListenAndServe(tsrv.sock)
|
||||
|
||||
waitForServer(t, tsrv.sock)
|
||||
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 (tsrv *testServer) Cleanup() {
|
||||
tsrv.Close()
|
||||
os.RemoveAll(tsrv.dir)
|
||||
}
|
||||
|
||||
func mkV(args ...string) url.Values {
|
||||
v := url.Values{}
|
||||
for i := 0; i < len(args); i += 2 {
|
||||
@@ -72,7 +63,6 @@ func mkV(args ...string) url.Values {
|
||||
|
||||
func TestEndToEnd(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
defer srv.Cleanup()
|
||||
|
||||
// Run the client.
|
||||
client := NewClient(srv.sock)
|
||||
@@ -112,21 +102,6 @@ func TestEndToEnd(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func waitForServer(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
for range 100 {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
conn, err := net.Dial("unix", path)
|
||||
if conn != nil {
|
||||
conn.Close()
|
||||
}
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatal("server didn't start")
|
||||
}
|
||||
|
||||
// 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(
|
||||
|
||||
Reference in New Issue
Block a user