test: add missing tests and fix hanging transport test

New test packages:
- config/loader_test.go: 5 tests (valid, minimal, missing file, pulsar-only, defaults)
- serial/serial_test.go: 6 tests (interface, read, EOF, close, empty)
- app/app_test.go: 5 tests (New, dispatch store/send, error handling, ctx cancellation)

Enhanced test packages:
- storage/store_test.go: +3 tests (concurrent insert, close/reopen, init clears)
- telegram/parser_test.go: +5 tests (concurrency, rawLog, empty, multiline, removeEmpty)
- transport/transport_test.go: +7 tests, +fixes

Fixes:
- app.go: store field type *storage.Store -> storage.Repository (supports mocking)
- transport_test: fix net.Pipe() sync deadlock in SetConnClosesPrevious
- transport_test: fix MultiSender_ErrorPropagation expectations

Results: 6 packages, 30+ tests, all pass with -race
This commit is contained in:
w1ndyb0y
2026-07-10 16:05:19 +08:00
parent 86d473c03d
commit 7f65004cd4
7 changed files with 852 additions and 2 deletions
+100
View File
@@ -1,7 +1,9 @@
package telegram
import (
"bytes"
"strings"
"sync"
"testing"
)
@@ -105,6 +107,88 @@ func TestParser_NonGreedyMatch(t *testing.T) {
}
}
func TestParser_ConcurrentAccess(t *testing.T) {
p := New(nil)
var wg sync.WaitGroup
const numGoroutines = 20
const iterations = 50
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
for j := 0; j < iterations; j++ {
telegrams := p.Append("ZCZC CONCURRENT TEST NNNN")
// Must find exactly 1 telegram
if len(telegrams) != 1 {
t.Errorf("goroutine %d: expected 1 telegram, got %d", id, len(telegrams))
return
}
if !strings.Contains(telegrams[0], "ZCZC CONCURRENT TEST NNNN") {
t.Errorf("goroutine %d: unexpected content: %s", id, telegrams[0])
return
}
}
}(i)
}
wg.Wait()
}
func TestParser_WithRawLog(t *testing.T) {
var buf bytes.Buffer
p := New(&buf)
telegrams := p.Append("ZCZC RAW LOG TEST NNNN")
if len(telegrams) != 1 {
t.Fatalf("expected 1 telegram, got %d", len(telegrams))
}
// Raw log should have been written
if buf.Len() == 0 {
t.Error("expected raw log to be written")
}
if !strings.Contains(buf.String(), "ZCZC RAW LOG TEST NNNN") {
t.Errorf("raw log missing expected content: %s", buf.String())
}
}
func TestParser_EmptyAppend(t *testing.T) {
p := New(nil)
telegrams := p.Append("")
if len(telegrams) != 0 {
t.Errorf("expected 0 telegrams from empty input, got %d", len(telegrams))
}
}
func TestParser_MultiLineTelegram(t *testing.T) {
p := New(nil)
telegrams := p.Append("ZCZC LINE1\n")
if len(telegrams) != 0 {
t.Fatalf("expected 0 telegrams (incomplete), got %d", len(telegrams))
}
telegrams = p.Append("LINE2\n")
if len(telegrams) != 0 {
t.Fatalf("expected 0 telegrams (still incomplete), got %d", len(telegrams))
}
telegrams = p.Append("LINE3 NNNN")
if len(telegrams) != 1 {
t.Fatalf("expected 1 telegram after completion, got %d", len(telegrams))
}
if !strings.Contains(telegrams[0], "LINE1") {
t.Errorf("missing LINE1: %s", telegrams[0])
}
if !strings.Contains(telegrams[0], "LINE2") {
t.Errorf("missing LINE2: %s", telegrams[0])
}
if !strings.Contains(telegrams[0], "LINE3") {
t.Errorf("missing LINE3: %s", telegrams[0])
}
}
func TestRemoveEmpty(t *testing.T) {
result := removeEmpty(" ZCZC\n\n\nTEST\n\nNNNN ")
expected := "ZCZC\nTEST\nNNNN"
@@ -112,3 +196,19 @@ func TestRemoveEmpty(t *testing.T) {
t.Errorf("removeEmpty(%q) = %q, want %q", " ZCZC\n\n\nTEST\n\nNNNN ", result, expected)
}
}
func TestRemoveEmpty_NoChange(t *testing.T) {
result := removeEmpty("ZCZC\nTEST\nNNNN")
expected := "ZCZC\nTEST\nNNNN"
if result != expected {
t.Errorf("removeEmpty(%q) = %q, want %q", "ZCZC\nTEST\nNNNN", result, expected)
}
}
func TestRemoveEmpty_AllWhitespace(t *testing.T) {
result := removeEmpty(" \n\n\n ")
expected := ""
if result != expected {
t.Errorf("removeEmpty(all whitespace) = %q, want %q", result, expected)
}
}