-
Notifications
You must be signed in to change notification settings - Fork 14
/
mock.go
126 lines (109 loc) · 2.71 KB
/
mock.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
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
/*
* Copyright 2021 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package dsl
import (
"bufio"
"io"
"strings"
"time"
)
func init() {
TheChanRegistry.Register(NewCtx(nil), "mock", NewMockChan)
}
// MockChan is a channel type that just emits what it receives.
//
// This channel type is mostly used for testing. A message published
// to a mock channel is simply emitted as is (for test to receive).
type MockChan struct {
c chan Msg
}
func NewMockChan(ctx *Ctx, _ interface{}) (Chan, error) {
return &MockChan{
c: make(chan Msg, 1024),
}, nil
}
func (c *MockChan) DocSpec() *DocSpec {
return &DocSpec{
Chan: &MockChan{},
}
}
func (c *MockChan) Kind() ChanKind {
return "mock"
}
func (c *MockChan) Open(ctx *Ctx) error {
return nil
}
func (c *MockChan) Close(ctx *Ctx) error {
return nil
}
func (c *MockChan) Sub(ctx *Ctx, topic string) error {
ctx.Logf("MockChan Sub %s", topic)
return nil
}
func (c *MockChan) Pub(ctx *Ctx, m Msg) error {
ctx.Logf("MockChan Pub topic %s", m.Topic)
ctx.Logdf(" payload %s", m.Payload)
return c.To(ctx, m)
}
func (c *MockChan) Recv(ctx *Ctx) chan Msg {
ctx.Logf("MockChan Recv")
return c.c
}
func (c *MockChan) Kill(ctx *Ctx) error {
return Brokenf("Kill is not supported by a %T", c)
}
func (c *MockChan) To(ctx *Ctx, m Msg) error {
ctx.Logf("MockChan To topic %s", m.Topic)
ctx.Logdf(" payload %s", m.Payload)
m.ReceivedAt = time.Now().UTC()
select {
case <-ctx.Done():
case c.c <- m:
default:
panic("Warning: MockChan channel full")
}
return nil
}
// Read is a utility function to read input for a MockChan.
//
// Does not close the reader.
func (c *MockChan) Read(ctx *Ctx, in *bufio.Reader) error {
ctx.Logf("MockChan reading input")
for {
line, err := in.ReadBytes('\n')
if err != nil && err != io.EOF {
return err
}
if len(line) == 0 && err == io.EOF {
return nil
}
parts := strings.SplitN(string(line), " ", 2)
if len(parts) != 2 {
ctx.Logf("error: MockChan.Read need topic payload")
continue
}
m := Msg{
Topic: parts[0],
Payload: parts[1],
}
if err = c.To(ctx, m); err != nil {
return err
}
}
return nil
}