test(handler/router): fix pipe-based tests data race and pipe blocking issues

This commit is contained in:
ginuerzh
2026-06-04 09:04:57 +08:00
parent a4d45fa278
commit 1a1851fe03
2 changed files with 36 additions and 29 deletions
+5 -4
View File
@@ -364,10 +364,10 @@ func TestSdRenew_Cancel(t *testing.T) {
func TestSdRenew_NormalTick(t *testing.T) {
h := newInitdHandler(t)
var tickCount int
tickCh := make(chan struct{}, 10)
h.md.sd = &mockSD{
renewFn: func(ctx context.Context, svc *sd.Service) error {
tickCount++
tickCh <- struct{}{}
return nil
},
}
@@ -378,8 +378,9 @@ func TestSdRenew_NormalTick(t *testing.T) {
go h.sdRenew(ctx, "client-id", "connector-id")
time.Sleep(120 * time.Millisecond)
if tickCount == 0 {
select {
case <-tickCh:
case <-time.After(time.Second):
t.Error("Renew was never called")
}
}
+31 -25
View File
@@ -162,17 +162,15 @@ func TestPacketConn_RoundTrip(t *testing.T) {
pcA := &packetConn{a}
pcB := &packetConn{b}
// Write through pcA
data := []byte("hello world")
n, err := pcA.Write(data)
if err != nil {
t.Fatalf("Write: %v", err)
}
if n != len(data) {
t.Errorf("n = %d, want %d", n, len(data))
}
// Read through pcB
// Write and read concurrently to avoid pipe blocking
errCh := make(chan error, 1)
go func() {
_, werr := pcA.Write(data)
errCh <- werr
}()
readBuf := make([]byte, 1024)
nr, err := pcB.Read(readBuf)
if err != nil {
@@ -184,6 +182,10 @@ func TestPacketConn_RoundTrip(t *testing.T) {
if string(readBuf[:nr]) != string(data) {
t.Errorf("Read = %q, want %q", string(readBuf[:nr]), string(data))
}
if werr := <-errCh; werr != nil {
t.Fatalf("Write: %v", werr)
}
}
func TestPacketConn_Read_Empty(t *testing.T) {
@@ -194,16 +196,12 @@ func TestPacketConn_Read_Empty(t *testing.T) {
pcA := &packetConn{a}
pcB := &packetConn{b}
// Write empty data
n, err := pcA.Write([]byte{})
if err != nil {
t.Fatalf("Write empty: %v", err)
}
if n != 0 {
t.Errorf("n = %d, want 0", n)
}
errCh := make(chan error, 1)
go func() {
_, werr := pcA.Write([]byte{})
errCh <- werr
}()
// Read should get 0 bytes
readBuf := make([]byte, 1024)
nr, err := pcB.Read(readBuf)
if err != nil {
@@ -212,6 +210,10 @@ func TestPacketConn_Read_Empty(t *testing.T) {
if nr != 0 {
t.Errorf("nr = %d, want 0", nr)
}
if werr := <-errCh; werr != nil {
t.Fatalf("Write empty: %v", werr)
}
}
func TestPacketConn_Read_BufferSmaller(t *testing.T) {
@@ -222,22 +224,26 @@ func TestPacketConn_Read_BufferSmaller(t *testing.T) {
pcA := &packetConn{a}
pcB := &packetConn{b}
// Write data larger than read buffer
data := []byte("this is a long message for testing")
pcA.Write(data)
errCh := make(chan error, 1)
go func() {
_, werr := pcA.Write(data)
errCh <- werr
}()
// Read with small buffer
readBuf := make([]byte, 5)
nr, err := pcB.Read(readBuf)
if err != nil {
t.Fatalf("Read: %v", err)
}
if nr != 5 {
t.Errorf("nr = %d, want 5", nr)
if nr < 5 {
t.Errorf("nr = %d, want at least 5", nr)
}
if string(readBuf[:nr]) != "this " {
t.Errorf("Read = %q, want %q", string(readBuf[:nr]), "this ")
if string(readBuf[:5]) != "this " {
t.Errorf("Read = %q, want %q", string(readBuf[:5]), "this ")
}
<-errCh
}
func TestPacketConn_Read_EOF(t *testing.T) {