-
Notifications
You must be signed in to change notification settings - Fork 20
/
deep_compare_archive_test.go
83 lines (77 loc) · 1.79 KB
/
deep_compare_archive_test.go
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
package ais_test
import (
"bufio"
"compress/gzip"
"github.com/BertoldVdb/go-ais"
"github.com/BertoldVdb/go-ais/aisnmea"
"io"
"os"
"reflect"
"testing"
"time"
)
func readAllPackets(t *testing.T, benchFile string, parseFast bool, cb func(eachPacket *aisnmea.VdmPacket)) {
c := ais.CodecNewFast(false, false, parseFast)
nm := aisnmea.NMEACodecNew(c)
var reader io.Reader
fp, err := os.Open(benchFile)
if err != nil {
t.Fatal("could not open ", benchFile, "\nerr:\n", err)
}
defer fp.Close()
reader, err = gzip.NewReader(fp)
if err != nil {
t.Fatal()
}
// create line by line scanner, read each line and decode it
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Bytes()
packet, err := nm.ParseSentence(string(line))
if err != nil {
t.Fatal("error parsing sentence", err)
}
cb(packet)
}
if err := scanner.Err(); err != nil {
t.Fatal("error reading file", err)
}
}
func TestDecodeFile(t *testing.T) {
file := os.Getenv("NMEA_BENCH_FILE")
if file == "" {
t.Skip("NMEA_BENCH_FILE not set")
}
slowChan := make(chan *aisnmea.VdmPacket, 1)
fastChan := make(chan *aisnmea.VdmPacket, 1)
finished := false
go func() {
readAllPackets(t, file, false, func(eachPacket *aisnmea.VdmPacket) {
slowChan <- eachPacket
})
finished = true
}()
go func() {
readAllPackets(t, file, false, func(eachPacket *aisnmea.VdmPacket) {
fastChan <- eachPacket
})
finished = true
}()
i := 0
for {
if finished {
break
}
cancelTimer := time.AfterFunc(10*time.Second, func() {
t.Fatal("Timeout should never happen")
os.Exit(1)
})
leftValue := <-slowChan
rightValue := <-fastChan
if !reflect.DeepEqual(leftValue, rightValue) {
t.Fatalf("Packet %d not equal (slow: %v, fast: %v)", i, leftValue, rightValue)
}
i += 1
cancelTimer.Stop()
}
}