-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_test.go
100 lines (85 loc) · 2.34 KB
/
helper_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package vote_test
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
"strings"
"time"
"github.com/inklabs/cqrs"
"github.com/inklabs/cqrs/asynccommandstore"
"github.com/inklabs/cqrs/pkg/clock/provider/incrementingclock"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
"github.com/inklabs/vote"
)
// TrimmingWriter trims trailing whitespace for each line before writing to the underlying writer.
// It also ignores multiple newlines due to this [issue](https://github.com/golang/go/issues/59191)
type TrimmingWriter struct {
w io.Writer
isLastLineEmpty bool
}
// NewTrimmingWriter creates a new TrimmingWriter.
func NewTrimmingWriter(w io.Writer) *TrimmingWriter {
return &TrimmingWriter{w: w}
}
// Write trims trailing whitespace for each line before writing to the underlying writer.
func (tw *TrimmingWriter) Write(p []byte) (n int, err error) {
scanner := bufio.NewScanner(bytes.NewReader(p))
for scanner.Scan() {
line := scanner.Text()
trimmedLine := strings.TrimRight(line, " \t\r\n")
if trimmedLine == "" {
if tw.isLastLineEmpty {
continue
}
tw.isLastLineEmpty = true
} else {
tw.isLastLineEmpty = false
}
_, err := tw.w.Write([]byte(trimmedLine + "\n"))
if err != nil {
return n, err
}
n += len(trimmedLine) + 1
}
return n, nil
}
func PrettyPrint(buf *bytes.Buffer) {
var prettyJSON bytes.Buffer
_ = json.Indent(&prettyJSON, buf.Bytes(), "", " ")
fmt.Print(prettyJSON.String())
}
func newTestApp(opts ...vote.Option) cqrs.App {
startTime := time.Unix(1699900000, 0)
seededClock := incrementingclock.New(startTime)
options := append([]vote.Option{
vote.WithAsyncCommandStore(asynccommandstore.NewInMemory()),
vote.WithClock(seededClock),
}, opts...)
return vote.NewApp(options...)
}
func startBufferedGRPCServer(grpcServer *grpc.Server) *grpc.ClientConn {
bufListener := bufconn.Listen(7)
go func() {
if err := grpcServer.Serve(bufListener); err != nil {
log.Fatalf("panic serving grpc: %v", err)
}
}()
dialer := grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return bufListener.Dial()
})
conn, _ := grpc.DialContext(
context.Background(),
"bufnet",
dialer,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
)
return conn
}