testing_internal.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. //go:build internal_testing
  14. // +build internal_testing
  15. // Functions in this file are only available when building nats.go with the
  16. // internal_testing build tag. They are used by the nats.go test suite.
  17. package nats
  18. // AddMsgFilter adds a message filter for the given subject
  19. // to the connection. The filter will be called for each
  20. // message received on the subject. If the filter returns
  21. // nil, the message will be dropped.
  22. func (nc *Conn) AddMsgFilter(subject string, filter msgFilter) {
  23. nc.subsMu.Lock()
  24. defer nc.subsMu.Unlock()
  25. if nc.filters == nil {
  26. nc.filters = make(map[string]msgFilter)
  27. }
  28. nc.filters[subject] = filter
  29. }
  30. // RemoveMsgFilter removes a message filter for the given subject.
  31. func (nc *Conn) RemoveMsgFilter(subject string) {
  32. nc.subsMu.Lock()
  33. defer nc.subsMu.Unlock()
  34. if nc.filters != nil {
  35. delete(nc.filters, subject)
  36. if len(nc.filters) == 0 {
  37. nc.filters = nil
  38. }
  39. }
  40. }
  41. // IsJSControlMessage returns true if the message is a JetStream control message.
  42. func IsJSControlMessage(msg *Msg) (bool, int) {
  43. return isJSControlMessage(msg)
  44. }
  45. // CloseTCPConn closes the underlying TCP connection.
  46. // It can be used to simulate a disconnect.
  47. func (nc *Conn) CloseTCPConn() {
  48. nc.mu.Lock()
  49. defer nc.mu.Unlock()
  50. nc.conn.Close()
  51. }