fix: comprehensive bug fixes and architecture restructuring

Phase 1 — Bug fixes (7 bugs):
- Bug 1: Pulsar mode data loss — always persist to SQLite regardless of mode
- Bug 2: PulsarSend closing TCP client — use independent resetPulsarProducer()
- Bug 3: Greedy regex — use non-greedy (?s)ZCZC.*?NNNN
- Bug 4: Data after NNNN discarded — keep remaining buffer data
- Bug 5: strings.Index > 0 boundary — use strings.Contains
- Bug 6: Variable shadowing in PulsarSend — use = not :=
- Bug 7: Accept failure nil panic — add continue + retry logic

Phase 2 — Architecture restructuring:
- Split utils/ into config/, serial/, telegram/, storage/, transport/
- Define Sender, Repository, Reader interfaces
- Introduce app/ layer with context.Context lifecycle
- Replace spinlock with sync.Mutex
- Unified Config struct replaces 15+ global vars

Phase 3 — Testing & tooling:
- telegram/parser_test.go (7 test cases)
- storage/store_test.go (5 test cases)
- transport/transport_test.go (4 test cases)
- Taskfile: add test, test-race, test-cover tasks
- Go version: 1.15 -> 1.21
This commit is contained in:
w1ndyb0y
2026-07-10 15:32:34 +08:00
parent 5f3334ec87
commit 419f25f0dd
20 changed files with 1519 additions and 206 deletions
+83
View File
@@ -0,0 +1,83 @@
package telegram
import (
"io"
"regexp"
"strings"
"sync"
)
const (
endTag = "NNNN"
expression = "(?s)ZCZC.*?NNNN"
maxBufferSize = 65536
bufferTrimSize = 32768
)
// Parser handles telegram extraction from raw serial data.
type Parser struct {
mu sync.Mutex
buffer strings.Builder
exp *regexp.Regexp
rawLog io.Writer
}
// New creates a new Parser.
func New(rawLog io.Writer) *Parser {
return &Parser{
exp: regexp.MustCompile(expression),
rawLog: rawLog,
}
}
// Append adds raw data to the internal buffer and returns any complete
// telegrams that were found. Returns nil if no complete telegram is ready.
func (p *Parser) Append(data string) []string {
p.mu.Lock()
defer p.mu.Unlock()
if p.buffer.Len() > maxBufferSize {
// Truncate to prevent unbounded growth
existing := p.buffer.String()
p.buffer.Reset()
p.buffer.WriteString(existing[len(existing)-bufferTrimSize:])
}
p.buffer.WriteString(data)
p.buffer.WriteByte('\n')
return p.extract()
}
// extract finds and returns all complete telegrams in the buffer.
func (p *Parser) extract() []string {
content := p.buffer.String()
if !strings.Contains(content, endTag) {
return nil
}
loc := p.exp.FindStringIndex(content)
if loc == nil {
return nil
}
telegram := content[loc[0]:loc[1]]
telegram = removeEmpty(telegram) + "\n\n\n"
// Write raw log
if p.rawLog != nil {
p.rawLog.Write([]byte(telegram + "\n"))
}
// Keep remaining data after the matched telegram
remaining := content[loc[1]:]
p.buffer.Reset()
p.buffer.WriteString(remaining)
return []string{telegram}
}
func removeEmpty(s string) string {
return regexp.MustCompile(`[\t\r\n]+`).ReplaceAllString(strings.TrimSpace(s), "\n")
}
+114
View File
@@ -0,0 +1,114 @@
package telegram
import (
"strings"
"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 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)
}
}