-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
149 lines (120 loc) · 4.27 KB
/
example_test.go
File metadata and controls
149 lines (120 loc) · 4.27 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrpssp_test
import (
"context"
"errors"
"fmt"
"io"
"strings"
"github.com/xmidt-org/wrp-go/v5"
wrpssp "github.com/xmidt-org/wrpssp/v2"
)
func Example() {
sent := "This is an example of how to use the wrpssp package."
packer, _ := wrpssp.New(
wrpssp.ID("123"),
wrpssp.Reader(strings.NewReader(sent)),
// Split the string into 5 byte packets for the example or there would
// only be one packet. Normally this would be a much larger number.
wrpssp.MaxPacketSize(5),
// Normally this would be EncodingGzip, but for the example we are using
// EncodingIdentity so that the packets are not compressed.
wrpssp.WithEncoding(wrpssp.EncodingIdentity),
)
assembler := wrpssp.Assembler{}
ctx := context.Background()
for {
// This is a pretty safe way to handle the packetizer. If the packetizer
// returns a msg, it should be sent. The error might be interesting,
// but it doesn't change what should be sent.
msg, err := packer.Next(ctx, wrp.Message{
Type: wrp.SimpleEventMessageType,
Source: "self:",
Destination: "event:foo",
})
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}
if msg == nil {
break
}
// Normally the message would have source, destination, and content type set
// but for this example we are only interested in the payload, so there
// is no need to set those fields in the msg.
// Normally the msg would be sent out over the wire, but for this
// example we are going to simply directly assemble the packets.
err = assembler.ProcessWRP(context.Background(), *msg)
if err != nil {
break
}
}
// The assembler will now have the original message.
buf, _ := io.ReadAll(&assembler)
fmt.Println(string(buf))
// Output: This is an example of how to use the wrpssp package.
}
func Example_requestResponse() {
sent := "This is an example of how to use the wrpssp package with a request/response."
// This allows a predictable transaction ID to be used for the example. Normally
// this would be a random UUID.
var tidCount int
packer, _ := wrpssp.New(
wrpssp.ID("123"),
wrpssp.Reader(strings.NewReader(sent)),
// Split the string into 5 byte packets for the example or there would
// only be one packet. Normally this would be a much larger number.
wrpssp.MaxPacketSize(15),
// Normally this would be EncodingGzip, but for the example we are using
// EncodingIdentity so that the packets are not compressed.
wrpssp.WithEncoding(wrpssp.EncodingIdentity),
// Normally you'd want to use a normal uuid generator, but for the example
// we are using a simple counter to generate a predictable transaction ID.
wrpssp.WithUpdateTransactionUUID(func() (string, error) {
defer func() {
tidCount++
}()
return fmt.Sprintf("tid-%d", tidCount), nil
}),
)
assembler := wrpssp.Assembler{}
ctx := context.Background()
for {
// This is a pretty safe way to handle the packetizer. If the packetizer
// returns a msg, it should be sent. The error might be interesting,
// but it doesn't change what should be sent.
msg, err := packer.Next(ctx, wrp.Message{
Type: wrp.SimpleRequestResponseMessageType,
Source: "self:",
Destination: "event:foo",
//TransactionUUID is automatically set by the packetizer.
})
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}
if msg == nil {
break
}
// Normally the message would have source, destination, and content type set
// but for this example we are only interested in the payload, so there
// is no need to set those fields in the msg.
fmt.Printf("Transaction ID: %s\n", msg.TransactionUUID)
// Normally the msg would be sent out over the wire, but for this
// example we are going to simply directly assemble the packets.
err = assembler.ProcessWRP(context.Background(), *msg)
if err != nil {
break
}
}
// The assembler will now have the original message.
buf, _ := io.ReadAll(&assembler)
fmt.Println(string(buf))
// Output:
// Transaction ID: tid-0
// Transaction ID: tid-1
// Transaction ID: tid-2
// Transaction ID: tid-3
// Transaction ID: tid-4
// Transaction ID: tid-5
// This is an example of how to use the wrpssp package with a request/response.
}