| 1 |
// Copyright 2026 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 |
// This file contains mock raft transport implementations and helper functions |
| 15 |
// for testing. The `raftTransportHub` manages multiple `mockTransport` instances, |
| 16 |
// allowing for the simulation of network partitions (`partition`, `heal`) |
| 17 |
// and message interception (`setAfterMsgHook`). This setup is used to test |
| 18 |
// raft interactions without actual network communication, providing control |
| 19 |
// over message delivery and network conditions. |
| 20 |
|
| 21 |
package server |
| 22 |
|
| 23 |
import ( |
| 24 |
"sync" |
| 25 |
) |
| 26 |
|
| 27 |
type msgHook func(subject, reply string, msg []byte) |
| 28 |
|
| 29 |
type raftTransportHub struct { |
| 30 |
mu sync.Mutex |
| 31 |
transports map[string]*mockTransport |
| 32 |
partitions map[string]int |
| 33 |
afterMsg msgHook |
| 34 |
} |
| 35 |
|
| 36 |
func newRaftTransportHub() *raftTransportHub { |
| 37 |
return &raftTransportHub{ |
| 38 |
partitions: make(map[string]int), |
| 39 |
transports: make(map[string]*mockTransport), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func (h *raftTransportHub) newTransport(s *Server, n RaftNode) raftTransport { |
| 44 |
return &mockTransport{ |
| 45 |
server: s, |
| 46 |
node: n, |
| 47 |
hub: h, |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func (h *raftTransportHub) register(t *mockTransport) { |
| 52 |
h.mu.Lock() |
| 53 |
defer h.mu.Unlock() |
| 54 |
h.transports[t.Node().ID()] = t |
| 55 |
} |
| 56 |
|
| 57 |
func (h *raftTransportHub) unregister(t *mockTransport) { |
| 58 |
h.mu.Lock() |
| 59 |
defer h.mu.Unlock() |
| 60 |
delete(h.transports, t.Node().ID()) |
| 61 |
} |
| 62 |
|
| 63 |
// Simulate a network partition. Nodes assigned to different `partitionID`s |
| 64 |
// will not be able to exchange messages, effectively isolating them. |
| 65 |
// By default, all nodes are considered to be in partition ID 0. |
| 66 |
// See `heal(nodeID)` and `healPartitions()` to heal a partition. |
| 67 |
func (h *raftTransportHub) partition(nodeID string, partitionID int) { |
| 68 |
h.mu.Lock() |
| 69 |
defer h.mu.Unlock() |
| 70 |
h.partitions[nodeID] = partitionID |
| 71 |
} |
| 72 |
|
| 73 |
// Reassign the node with the given ID to the default partition 0. |
| 74 |
func (h *raftTransportHub) heal(nodeID string) { |
| 75 |
h.mu.Lock() |
| 76 |
defer h.mu.Unlock() |
| 77 |
delete(h.partitions, nodeID) |
| 78 |
} |
| 79 |
|
| 80 |
// Reassign all nodes to the default partition 0. |
| 81 |
func (h *raftTransportHub) healPartitions() { |
| 82 |
h.mu.Lock() |
| 83 |
defer h.mu.Unlock() |
| 84 |
clear(h.partitions) |
| 85 |
} |
| 86 |
|
| 87 |
// Set a hook that is called back after a message is published. The after |
| 88 |
// message hook can expect the raftTransportHub to be unlocked. It is OK |
| 89 |
// to interact with the raftTransportHub inside the message hook. On the |
| 90 |
// other hand, the underlying RaftNode that has sent the message remains |
| 91 |
// locked throughout the execution of the hook. The hook can only call |
| 92 |
// methods that do not lock the RaftNode. |
| 93 |
func (h *raftTransportHub) setAfterMsgHook(hook msgHook) { |
| 94 |
h.mu.Lock() |
| 95 |
defer h.mu.Unlock() |
| 96 |
h.afterMsg = hook |
| 97 |
} |
| 98 |
|
| 99 |
func (h *raftTransportHub) publish(t *mockTransport, subject, reply string, msg []byte) { |
| 100 |
h.mu.Lock() |
| 101 |
afterMsgHook := h.afterMsg |
| 102 |
sender := t.Node().ID() |
| 103 |
partition := h.partitions[sender] |
| 104 |
|
| 105 |
for id, transport := range h.transports { |
| 106 |
if sender == id { |
| 107 |
continue |
| 108 |
} |
| 109 |
|
| 110 |
if partition != h.partitions[id] { |
| 111 |
continue |
| 112 |
} |
| 113 |
|
| 114 |
res := transport.sub.Match(subject) |
| 115 |
for _, sub := range res.psubs { |
| 116 |
sub.icb(sub, nil, transport.acc, subject, reply, msg) |
| 117 |
} |
| 118 |
} |
| 119 |
h.mu.Unlock() |
| 120 |
|
| 121 |
if afterMsgHook != nil { |
| 122 |
afterMsgHook(subject, reply, msg) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
type mockTransport struct { |
| 127 |
server *Server |
| 128 |
node RaftNode |
| 129 |
hub *raftTransportHub |
| 130 |
sub *Sublist |
| 131 |
acc *Account |
| 132 |
} |
| 133 |
|
| 134 |
func (t *mockTransport) Reset(acc *Account) { |
| 135 |
t.Close() |
| 136 |
t.acc = acc |
| 137 |
t.sub = NewSublist(false) |
| 138 |
t.hub.register(t) |
| 139 |
} |
| 140 |
|
| 141 |
func (t *mockTransport) Close() { |
| 142 |
t.hub.unregister(t) |
| 143 |
t.sub = nil |
| 144 |
} |
| 145 |
|
| 146 |
func (t *mockTransport) Node() RaftNode { |
| 147 |
return t.node |
| 148 |
} |
| 149 |
|
| 150 |
func (t *mockTransport) Account() *Account { |
| 151 |
return t.acc |
| 152 |
} |
| 153 |
|
| 154 |
func (t *mockTransport) Publish(subject string, reply string, msg []byte) { |
| 155 |
t.hub.publish(t, subject, reply, msg) |
| 156 |
} |
| 157 |
|
| 158 |
func (t *mockTransport) Subscribe(subject string, cb msgHandler) (*subscription, error) { |
| 159 |
if t.sub == nil { |
| 160 |
return nil, errNoInternalClient |
| 161 |
} |
| 162 |
sub := &subscription{subject: []byte(subject), sid: nil, icb: cb} |
| 163 |
if err := t.sub.Insert(sub); err != nil { |
| 164 |
return nil, err |
| 165 |
} |
| 166 |
return sub, nil |
| 167 |
} |
| 168 |
|
| 169 |
func (t *mockTransport) Unsubscribe(sub *subscription) { |
| 170 |
if t.sub != nil { |
| 171 |
t.sub.Remove(sub) |
| 172 |
} |
| 173 |
} |