-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_file_test.go
More file actions
93 lines (78 loc) · 2.63 KB
/
Copy pathsource_file_test.go
File metadata and controls
93 lines (78 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package n2k
import (
"context"
"os"
"path/filepath"
"testing"
"time"
"github.com/open-ships/n2k/pgn"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// headingLine is a candump -L line carrying a VesselHeading (PGN 127250)
// frame: SID=1, Heading=15708 (1.5708 rad), deviation/variation null,
// reference=True.
const headingLine = "(1720000000.000000) can0 09F11201#015C3DFF7FFF7FFC"
func writeTempLog(t *testing.T, content string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "capture.log")
require.NoError(t, os.WriteFile(path, []byte(content), 0o600))
return path
}
func TestFileSource_DecodesCandumpLog(t *testing.T) {
path := writeTempLog(t, headingLine+"\n# a comment\n\n")
var msgs []pgn.Message
for msg, err := range Receive(context.Background(), File(path)) {
require.NoError(t, err)
msgs = append(msgs, msg)
}
require.Len(t, msgs, 1)
vh, ok := msgs[0].(*pgn.VesselHeading)
require.True(t, ok, "expected *pgn.VesselHeading, got %T", msgs[0])
require.NotNil(t, vh.Heading)
assert.Equal(t, uint64(15708), *vh.Heading)
assert.Equal(t, uint8(0x01), vh.Info.SourceId)
}
func TestFileSource_OriginalTimingPacesFrames(t *testing.T) {
log := "(1720000000.000000) can0 09F11201#015C3DFF7FFF7FFC\n" +
"(1720000000.080000) can0 09F11201#025C3DFF7FFF7FFC\n"
path := writeTempLog(t, log)
start := time.Now()
count := 0
for _, err := range Receive(context.Background(), File(path, OriginalTiming())) {
require.NoError(t, err)
count++
}
elapsed := time.Since(start)
require.Equal(t, 2, count)
assert.GreaterOrEqual(t, elapsed, 60*time.Millisecond,
"OriginalTiming should sleep ~80ms between the two frames")
}
func TestFileSource_MissingFile(t *testing.T) {
var firstErr error
for _, err := range Receive(context.Background(), File(filepath.Join(t.TempDir(), "nope.log"))) {
if err != nil {
firstErr = err
break
}
}
require.Error(t, firstErr)
}
func TestFileSource_ContextCancelStopsPacing(t *testing.T) {
// Two frames 10 minutes apart; cancellation must interrupt the sleep.
log := "(1720000000.000000) can0 09F11201#015C3DFF7FFF7FFC\n" +
"(1720000600.000000) can0 09F11201#025C3DFF7FFF7FFC\n"
path := writeTempLog(t, log)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
start := time.Now()
for range Receive(ctx, File(path, OriginalTiming())) {
}
assert.Less(t, time.Since(start), 5*time.Second)
}
func TestNewClient_RejectsReadOnlySource(t *testing.T) {
path := writeTempLog(t, headingLine+"\n")
_, err := NewClient(context.Background(), File(path))
require.Error(t, err)
assert.Contains(t, err.Error(), "read-only")
}