Debian Codegraph @

testing_internal.go

raw
1 // Copyright 2023 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 //go:build internal_testing
15
16 // Functions in this file are only available when building nats.go with the
17 // internal_testing build tag. They are used by the nats.go test suite.
18 package nats
19
20 // AddMsgFilter adds a message filter for the given subject
21 // to the connection. The filter will be called for each
22 // message received on the subject. If the filter returns
23 // nil, the message will be dropped.
24 func (nc *Conn) AddMsgFilter(subject string, filter msgFilter) {
25 nc.subsMu.Lock()
26 defer nc.subsMu.Unlock()
27
28 if nc.filters == nil {
29 nc.filters = make(map[string]msgFilter)
30 }
31 nc.filters[subject] = filter
32 }
33
34 // RemoveMsgFilter removes a message filter for the given subject.
35 func (nc *Conn) RemoveMsgFilter(subject string) {
36 nc.subsMu.Lock()
37 defer nc.subsMu.Unlock()
38
39 if nc.filters != nil {
40 delete(nc.filters, subject)
41 if len(nc.filters) == 0 {
42 nc.filters = nil
43 }
44 }
45 }
46
47 // IsJSControlMessage returns true if the message is a JetStream control message.
48 func IsJSControlMessage(msg *Msg) (bool, int) {
49 return isJSControlMessage(msg)
50 }
51
52 // CloseTCPConn closes the underlying TCP connection.
53 // It can be used to simulate a disconnect.
54 func (nc *Conn) CloseTCPConn() {
55 nc.mu.Lock()
56 defer nc.mu.Unlock()
57 nc.conn.Close()
58 }