1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-10-28 15:02:28 +00:00
Files
go-chasquid-smtp/internal/nettrace/context_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

66 lines
1.5 KiB
Go

package nettrace
import (
"testing"
)
func TestContext(t *testing.T) {
tr := New("TestContext", "trace")
defer tr.Finish()
// Attach the trace to a new context.
ctx := NewContext(t.Context(), tr)
// Get the trace back from the context.
{
tr2, ok := FromContext(ctx)
if !ok {
t.Errorf("Context with trace returned not found")
}
if tr != tr2 {
t.Errorf("Trace from context is different: %v != %v", tr, tr2)
}
}
// Create a child trace from the context.
{
tr3 := ChildFromContext(ctx, "TestContext", "child")
if p := tr3.(*trace).Parent; p != tr {
t.Errorf("Child doesn't have the right parent: %v != %v", p, tr)
}
tr3.Finish()
}
// FromContextOrNew returns the one from the context.
{
tr4, ctx4 := FromContextOrNew(ctx, "TestContext", "from-ctx")
if ctx4 != ctx {
t.Errorf("Got new context: %v != %v", ctx4, ctx)
}
if tr4 != tr {
t.Errorf("Context with trace returned new trace: %v != %v", tr4, tr)
}
}
// FromContextOrNew needs to create a new one.
{
tr5, ctx5 := FromContextOrNew(
t.Context(), "TestContext", "tr5")
if tr, _ := FromContext(ctx5); tr != tr5 {
t.Errorf("Context with trace returned the wrong trace: %v != %v",
tr, tr5)
}
tr5.Finish()
}
// Child from a context that has no trace attached.
{
tr6 := ChildFromContext(
t.Context(), "TestContext", "child")
tr6.Finish()
if p := tr6.(*trace).Parent; p != nil {
t.Errorf("Expected orphan trace, it has a parent: %v", p)
}
}
}