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
214 lines
6.0 KiB
Go
214 lines
6.0 KiB
Go
package telegram
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestParser_SingleTelegram(t *testing.T) {
|
|
p := New(nil)
|
|
telegrams := p.Append("ZCZC TEST MESSAGE NNNN")
|
|
if len(telegrams) != 1 {
|
|
t.Fatalf("expected 1 telegram, got %d", len(telegrams))
|
|
}
|
|
if !strings.Contains(telegrams[0], "ZCZC TEST MESSAGE NNNN") {
|
|
t.Errorf("unexpected telegram content: %s", telegrams[0])
|
|
}
|
|
}
|
|
|
|
func TestParser_MultipleTelegrams(t *testing.T) {
|
|
p := New(nil)
|
|
// Simulate two telegrams arriving in one burst
|
|
telegrams := p.Append("ZCZC MSG1 NNNN ZCZC MSG2 NNNN")
|
|
if len(telegrams) != 1 {
|
|
t.Fatalf("expected 1 telegram (non-greedy stops at first NNNN), got %d", len(telegrams))
|
|
}
|
|
if !strings.Contains(telegrams[0], "ZCZC MSG1 NNNN") {
|
|
t.Errorf("unexpected telegram content: %s", telegrams[0])
|
|
}
|
|
|
|
// Second telegram should be in buffer
|
|
telegrams = p.Append("")
|
|
if len(telegrams) != 1 {
|
|
t.Fatalf("expected 1 telegram from remaining buffer, got %d", len(telegrams))
|
|
}
|
|
if !strings.Contains(telegrams[0], "ZCZC MSG2 NNNN") {
|
|
t.Errorf("unexpected telegram content: %s", telegrams[0])
|
|
}
|
|
}
|
|
|
|
func TestParser_NNNNInBufferStart(t *testing.T) {
|
|
p := New(nil)
|
|
// Simulate leftover NNNN from previous truncation
|
|
telegrams := p.Append("NNNN ZCZC TEST NNNN")
|
|
if len(telegrams) != 1 {
|
|
t.Fatalf("expected 1 telegram, got %d", len(telegrams))
|
|
}
|
|
if !strings.Contains(telegrams[0], "ZCZC TEST NNNN") {
|
|
t.Errorf("unexpected telegram content: %s", telegrams[0])
|
|
}
|
|
}
|
|
|
|
func TestParser_NoTelegram(t *testing.T) {
|
|
p := New(nil)
|
|
telegrams := p.Append("some random noise without markers")
|
|
if len(telegrams) != 0 {
|
|
t.Fatalf("expected 0 telegrams, got %d", len(telegrams))
|
|
}
|
|
}
|
|
|
|
func TestParser_PartialTelegram(t *testing.T) {
|
|
p := New(nil)
|
|
// First line: start of telegram
|
|
telegrams := p.Append("ZCZC PARTIAL")
|
|
if len(telegrams) != 0 {
|
|
t.Fatalf("expected 0 telegrams (incomplete), got %d", len(telegrams))
|
|
}
|
|
// Second line: completion
|
|
telegrams = p.Append("CONTINUES HERE NNNN")
|
|
if len(telegrams) != 1 {
|
|
t.Fatalf("expected 1 telegram after completion, got %d", len(telegrams))
|
|
}
|
|
if !strings.Contains(telegrams[0], "ZCZC PARTIAL") || !strings.Contains(telegrams[0], "CONTINUES HERE NNNN") {
|
|
t.Errorf("unexpected telegram content: %s", telegrams[0])
|
|
}
|
|
}
|
|
|
|
func TestParser_BufferOverflow(t *testing.T) {
|
|
p := New(nil)
|
|
// Fill buffer beyond maxBufferSize
|
|
largeData := strings.Repeat("A", maxBufferSize+100)
|
|
telegrams := p.Append(largeData)
|
|
if len(telegrams) != 0 {
|
|
t.Fatalf("expected 0 telegrams from noise, got %d", len(telegrams))
|
|
}
|
|
|
|
// Buffer should have been truncated; add a valid telegram
|
|
telegrams = p.Append("ZCZC AFTER OVERFLOW NNNN")
|
|
if len(telegrams) != 1 {
|
|
t.Fatalf("expected 1 telegram after overflow, got %d", len(telegrams))
|
|
}
|
|
}
|
|
|
|
func TestParser_NonGreedyMatch(t *testing.T) {
|
|
p := New(nil)
|
|
// Two telegrams in same line - non-greedy should capture first only
|
|
telegrams := p.Append("ZCZC FIRST NNNN ZCZC SECOND NNNN")
|
|
if len(telegrams) != 1 {
|
|
t.Fatalf("expected 1 telegram, got %d", len(telegrams))
|
|
}
|
|
if !strings.Contains(telegrams[0], "ZCZC FIRST NNNN") {
|
|
t.Errorf("expected first telegram, got: %s", telegrams[0])
|
|
}
|
|
if strings.Contains(telegrams[0], "SECOND") {
|
|
t.Errorf("non-greedy match should not include second telegram: %s", telegrams[0])
|
|
}
|
|
}
|
|
|
|
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"
|
|
if result != expected {
|
|
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)
|
|
}
|
|
} |