mirror of
https://github.com/soypat/lneto.git
synced 2026-08-22 07:29:04 +00:00
done for today :)
This commit is contained in:
+67
-15
@@ -11,21 +11,37 @@ func TestTxQueue(t *testing.T) {
|
||||
rng := rand.New(rand.NewSource(1))
|
||||
|
||||
var rtx ringTx
|
||||
t.Run("SequentialMessages", func(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
rng.Read(msgBuf[:])
|
||||
msgs := bytes.SplitAfter(msgBuf[:], []byte{0})
|
||||
testTxQueue_SequentialMessages(t, &rtx, msgs, buf[:], aux[:], rng.Intn(4)+1, Value(rng.Int()))
|
||||
increasingComplexityTests := []struct {
|
||||
name string
|
||||
test func(*testing.T)
|
||||
}{
|
||||
0: {
|
||||
name: "SequentialMessages",
|
||||
test: func(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
rng.Read(msgBuf[:])
|
||||
msgs := bytes.SplitAfter(msgBuf[:], []byte{0})
|
||||
testTxQueue_SequentialMessages(t, &rtx, msgs, buf[:], aux[:], rng.Intn(4)+1, 0)
|
||||
}
|
||||
},
|
||||
},
|
||||
1: {
|
||||
name: "N-Messages",
|
||||
test: func(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
rng.Read(msgBuf[:])
|
||||
msgs := bytes.SplitAfter(msgBuf[:], []byte{0})
|
||||
testTxQueue_NMessages(t, &rtx, msgs, buf[:], aux[:], len(msgs), 0)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
for i, test := range increasingComplexityTests {
|
||||
t.Run(test.name, test.test)
|
||||
if t.Failed() {
|
||||
t.Fatalf("subtest %d/%d %q failed, not running more complex tests until fixed", i+1, len(increasingComplexityTests), test.name)
|
||||
}
|
||||
})
|
||||
t.Run("N-Messages", func(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
rng.Read(msgBuf[:])
|
||||
msgs := bytes.SplitAfter(msgBuf[:], []byte{0})
|
||||
|
||||
testTxQueue_NMessages(t, &rtx, msgs, buf[:], aux[:], len(msgs), Value(rng.Int()))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testTxQueue_NMessages(t *testing.T, rtx *ringTx, msgs [][]byte, buf, aux []byte, maxPkt int, startAck Value) {
|
||||
@@ -53,10 +69,12 @@ func testTxQueue_NMessages(t *testing.T, rtx *ringTx, msgs [][]byte, buf, aux []
|
||||
} else if n != len(msg) {
|
||||
t.Fatalf("want %d written, got %d", len(msg), n)
|
||||
}
|
||||
testQueueSanity(t, rtx)
|
||||
unsent := rtx.Buffered()
|
||||
if unsent != n {
|
||||
t.Fatalf("want unset %d, got %d", n, unsent)
|
||||
}
|
||||
testQueueSanity(t, rtx)
|
||||
n, seq, err := rtx.MakePacket(aux[sent : sent+len(msg)])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -65,10 +83,12 @@ func testTxQueue_NMessages(t *testing.T, rtx *ringTx, msgs [][]byte, buf, aux []
|
||||
} else if n != len(msg) {
|
||||
t.Fatalf("want full message %d sent, got %d", len(msg), n)
|
||||
}
|
||||
testQueueSanity(t, rtx)
|
||||
gotSent := rtx.BufferedSent()
|
||||
if gotSent != sent+n {
|
||||
t.Fatalf("want sent %d, got %d", sent+n, gotSent)
|
||||
}
|
||||
testQueueSanity(t, rtx)
|
||||
packets = append(packets, aux[sent:sent+n])
|
||||
prevSeq += Value(n)
|
||||
sent += n
|
||||
@@ -91,14 +111,17 @@ func testTxQueue_SequentialMessages(t *testing.T, rtx *ringTx, msgs [][]byte, bu
|
||||
} else if n != len(msg) {
|
||||
t.Fatalf("want %d written, got %d", len(msg), n)
|
||||
}
|
||||
testQueueSanity(t, rtx)
|
||||
unsent := rtx.Buffered()
|
||||
if len(msg) != unsent {
|
||||
t.Fatalf("want %d unsent buffered, got %d", unsent, len(msg))
|
||||
t.Fatalf("want %d unsent buffered, got %d", len(msg), unsent)
|
||||
}
|
||||
testQueueSanity(t, rtx)
|
||||
sent := rtx.BufferedSent()
|
||||
if sent != 0 {
|
||||
t.Fatalf("want 0 bytes sent, got %d", sent)
|
||||
}
|
||||
testQueueSanity(t, rtx)
|
||||
n, seq, err := rtx.MakePacket(aux[:])
|
||||
data := aux[:n]
|
||||
if err != nil {
|
||||
@@ -110,14 +133,43 @@ func testTxQueue_SequentialMessages(t *testing.T, rtx *ringTx, msgs [][]byte, bu
|
||||
} else if seq != prevSeq {
|
||||
t.Fatalf("want seq %d, got %d", prevSeq, seq)
|
||||
}
|
||||
testQueueSanity(t, rtx)
|
||||
sent = rtx.BufferedSent()
|
||||
if sent != len(msg) {
|
||||
t.Fatalf("want %d sent, got %d", len(msg), sent)
|
||||
}
|
||||
testQueueSanity(t, rtx)
|
||||
prevSeq += Value(n)
|
||||
err = rtx.RecvACK(prevSeq)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
testQueueSanity(t, rtx)
|
||||
}
|
||||
}
|
||||
|
||||
func testQueueSanity(t *testing.T, rtx *ringTx) {
|
||||
t.Helper()
|
||||
if rtx.emptyRing != (ringidx{}) {
|
||||
t.Fatalf("empty ring not empty")
|
||||
}
|
||||
free := rtx.Free()
|
||||
sent := rtx.BufferedSent()
|
||||
unsent := rtx.Buffered()
|
||||
sz := rtx.Size()
|
||||
gotSz := free + sent + unsent
|
||||
if gotSz != sz {
|
||||
t.Fatalf("want size=%d, got size=%d (free+sent+unsent=%d+%d+%d)", sz, gotSz, free, sent, unsent)
|
||||
}
|
||||
freeStart, freeEnd, sentEnd := rtx.lims()
|
||||
gotFreeEnd := rtx.addOff(freeStart, free)
|
||||
gotSentEnd := rtx.addOff(freeEnd, sent)
|
||||
gotUnsentEnd := rtx.addOff(sentEnd, unsent)
|
||||
if free != 0 && gotFreeEnd != freeEnd {
|
||||
t.Fatalf("want freeEnd=%d, got %d", freeEnd, gotFreeEnd)
|
||||
} else if sent != 0 && gotSentEnd != sentEnd {
|
||||
t.Fatalf("want sentEnd=%d, got %d", sentEnd, gotSentEnd)
|
||||
} else if unsent != 0 && gotUnsentEnd != freeStart {
|
||||
t.Fatalf("want unsentEnd=%d, got %d (freeStart)", freeStart, gotUnsentEnd)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user