|
1 | 1 | /******************************************************************************* |
2 | 2 |
|
3 | | - Test of the abstract turtle node extension. |
| 3 | + Test of the turtle swarm node extension. |
4 | 4 |
|
5 | 5 | Copyright: |
6 | 6 | Copyright (c) 2018 dunnhumby Germany GmbH. All rights reserved. |
|
12 | 12 |
|
13 | 13 | module integrationtest.turtleenv.main; |
14 | 14 |
|
15 | | -import swarm.neo.AddrPort; |
16 | | -import turtle.env.model.Node; |
| 15 | +import ocean.transition; |
| 16 | +import ocean.task.Scheduler; |
| 17 | +import ocean.task.Task; |
17 | 18 | import ocean.util.test.DirectorySandbox; |
18 | | -import ocean.core.Test; |
19 | | -import ocean.io.device.File; |
| 19 | +import swarm.Const : ICommandCodes, NodeItem; |
| 20 | +import swarm.node.connection.ConnectionHandler; |
| 21 | +import turtle.env.model.TestNode; |
20 | 22 |
|
21 | | -/// Node used to the turtle test |
22 | | -private class TurtleNode |
| 23 | +version (UnitTest){} |
| 24 | +else |
| 25 | +void main() |
23 | 26 | { |
24 | | - /// Address and the port of the node |
25 | | - AddrPort addrport; |
26 | | - AddrPort neo_address; |
| 27 | + auto sandbox = DirectorySandbox.create(); |
| 28 | + scope (success) |
| 29 | + sandbox.remove(); |
27 | 30 |
|
28 | | - this (AddrPort addrport) |
29 | | - { |
30 | | - this.addrport = addrport; |
31 | | - this.neo_address = AddrPort(this.addrport.address()); |
32 | | - this.neo_address.port = cast(ushort)(this.addrport.port() + 100); |
33 | | - } |
| 31 | + initScheduler(Scheduler.Configuration.init); |
| 32 | + |
| 33 | + auto node = new MyNode("127.0.0.1", 10000); |
| 34 | + node.start(); |
| 35 | + |
| 36 | + theScheduler.schedule(new Tests(node)); |
| 37 | + theScheduler.eventLoop(); |
34 | 38 | } |
35 | 39 |
|
36 | | -/// The turlte TurtleNode class |
37 | | -private class TestNode : Node!(TurtleNode, "turtleNode") |
| 40 | +/******************************************************************************* |
| 41 | +
|
| 42 | + Task that performs tests on the test node passed to the ctor. |
| 43 | +
|
| 44 | +*******************************************************************************/ |
| 45 | + |
| 46 | +class Tests : Task |
38 | 47 | { |
39 | | - /*********************************************************************** |
| 48 | + import integrationtest.neo.client.Client; |
| 49 | + import ocean.core.Test; |
| 50 | + import ocean.io.device.File; |
| 51 | + |
| 52 | + /// Node instance to test. |
| 53 | + private MyNode node; |
| 54 | + |
| 55 | + /// Client instance to use for checking network availability of the node. |
| 56 | + private Client client; |
40 | 57 |
|
41 | | - Creates a fake node at the specified address/port. |
| 58 | + /*************************************************************************** |
| 59 | +
|
| 60 | + Constructor. |
42 | 61 |
|
43 | 62 | Params: |
44 | | - node_item = address/port |
| 63 | + node = node to test |
| 64 | +
|
| 65 | + ***************************************************************************/ |
| 66 | + |
| 67 | + public this ( MyNode node ) |
| 68 | + { |
| 69 | + this.node = node; |
| 70 | + } |
| 71 | + |
| 72 | + /*************************************************************************** |
45 | 73 |
|
46 | | - ***********************************************************************/ |
| 74 | + Task entry point. Runs a series of tests on the node then shuts down the |
| 75 | + scheduler. |
47 | 76 |
|
48 | | - override protected TurtleNode createNode ( AddrPort addrport ) |
| 77 | + ***************************************************************************/ |
| 78 | + |
| 79 | + public override void run ( ) |
49 | 80 | { |
50 | | - return new TurtleNode(addrport); |
| 81 | + // Test config file generation. |
| 82 | + this.node.genConfigFiles("."); |
| 83 | + test!("==")(File.get("testnode.nodes"), "127.0.0.1:9999\n"); |
| 84 | + test!("==")(File.get("testnode.neo.nodes"), "127.0.0.1:10000\n"); |
| 85 | + |
| 86 | + // Initialise client and connect. |
| 87 | + this.client = new Client(theScheduler.epoll, "127.0.0.1", 10000, |
| 88 | + &this.connNotifier); |
| 89 | + client.blocking.waitAllNodesConnected(); |
| 90 | + |
| 91 | + // Try to talk to the node. |
| 92 | + auto ok = this.talkToNode(); |
| 93 | + test(ok); |
| 94 | + |
| 95 | + // Stop the node, then try to talk to it (failure expected). |
| 96 | + this.node.stop(); |
| 97 | + ok = this.talkToNode(); |
| 98 | + test(!ok); |
| 99 | + |
| 100 | + // Restart the node, reconnect the client, then try to talk to the node. |
| 101 | + this.node.restart(); |
| 102 | + client.blocking.waitAllNodesConnected(); |
| 103 | + ok = this.talkToNode(); |
| 104 | + test(ok); |
| 105 | + |
| 106 | + // Finished. |
| 107 | + theScheduler.shutdown(); |
51 | 108 | } |
52 | 109 |
|
53 | | - /*********************************************************************** |
| 110 | + /*************************************************************************** |
| 111 | +
|
| 112 | + Uses the client to put a record to the node, then read it back. |
54 | 113 |
|
55 | 114 | Returns: |
56 | | - address/port on which node is listening |
| 115 | + true if everything succeeded, false on error |
57 | 116 |
|
58 | | - ***********************************************************************/ |
| 117 | + ***************************************************************************/ |
59 | 118 |
|
60 | | - override public AddrPort node_addrport ( ) |
| 119 | + private bool talkToNode ( ) |
61 | 120 | { |
62 | | - assert(this.node); |
63 | | - return this.node.addrport; |
| 121 | + auto ok = client.blocking.put(1, "hello", |
| 122 | + ( Client.Neo.Put.Notification, Const!(Client.Neo.Put.Args) ) { }); |
| 123 | + if ( !ok ) |
| 124 | + return false; |
| 125 | + |
| 126 | + void[] value; |
| 127 | + ok = client.blocking.get(1, value, |
| 128 | + ( Client.Neo.Get.Notification, Const!(Client.Neo.Get.Args) ) { }); |
| 129 | + if ( !ok ) |
| 130 | + return false; |
| 131 | + |
| 132 | + return true; |
64 | 133 | } |
65 | 134 |
|
66 | | - /*********************************************************************** |
| 135 | + /*************************************************************************** |
67 | 136 |
|
68 | | - Fake node service stop implementation. |
| 137 | + Dummy connection notifier. Required by the client, but unused. |
69 | 138 |
|
70 | | - ***********************************************************************/ |
| 139 | + ***************************************************************************/ |
71 | 140 |
|
72 | | - protected override void stopImpl ( ) |
| 141 | + private void connNotifier ( Client.Neo.ConnNotification info ) |
73 | 142 | { |
74 | 143 | } |
| 144 | +} |
| 145 | + |
| 146 | +/******************************************************************************* |
| 147 | +
|
| 148 | + Test node implementing the protocol defined in integrationtest.neo.node. |
| 149 | +
|
| 150 | +*******************************************************************************/ |
| 151 | + |
| 152 | +public class MyNode : TestNode!(ConnHandler) |
| 153 | +{ |
| 154 | + import swarm.neo.AddrPort; |
75 | 155 |
|
76 | | - /*********************************************************************** |
| 156 | + import integrationtest.neo.node.Node; |
| 157 | + import integrationtest.neo.node.Storage; |
| 158 | + import integrationtest.neo.node.request.Get; |
| 159 | + import integrationtest.neo.node.request.Put; |
| 160 | + |
| 161 | + /*************************************************************************** |
| 162 | +
|
| 163 | + Constructor. |
| 164 | +
|
| 165 | + Params: |
| 166 | + addr = address to bind to |
| 167 | + neo_port = port to bind to for neo protocol (legacy protocol binds |
| 168 | + to a port one lower) |
| 169 | +
|
| 170 | + ***************************************************************************/ |
| 171 | + |
| 172 | + public this ( cstring addr, ushort neo_port ) |
| 173 | + { |
| 174 | + // In this simple example node implementation, we don't need any shared |
| 175 | + // resources except the reference to the storage. |
| 176 | + this.shared_resources = new Storage; |
| 177 | + |
| 178 | + Options options; |
| 179 | + options.epoll = theScheduler.epoll; |
| 180 | + options.requests.addHandler!(GetImpl_v0)(); |
| 181 | + options.credentials_map["dummy"] = Key.init; |
| 182 | + options.shared_resources = this.shared_resources; |
| 183 | + |
| 184 | + options.requests.addHandler!(GetImpl_v0)(); |
| 185 | + options.requests.addHandler!(PutImpl_v0)(); |
| 186 | + |
| 187 | + const backlog = 1_000; |
| 188 | + AddrPort legacy_addr_port; |
| 189 | + legacy_addr_port.setAddress(addr); |
| 190 | + legacy_addr_port.port = cast(ushort)(neo_port - 1); |
| 191 | + super(legacy_addr_port, neo_port, new ConnectionSetupParams, |
| 192 | + options, backlog); |
| 193 | + } |
| 194 | + |
| 195 | + /*************************************************************************** |
77 | 196 |
|
78 | 197 | Removes all data from the fake node service. |
79 | 198 |
|
80 | | - ***********************************************************************/ |
| 199 | + ***************************************************************************/ |
81 | 200 |
|
82 | 201 | override public void clear ( ) |
83 | 202 | { |
| 203 | + // Unused in this test. |
| 204 | + } |
| 205 | + |
| 206 | + /*************************************************************************** |
| 207 | +
|
| 208 | + Returns: |
| 209 | + identifier string for this node |
| 210 | +
|
| 211 | + ***************************************************************************/ |
| 212 | + |
| 213 | + protected override cstring id ( ) |
| 214 | + { |
| 215 | + return "testnode"; |
84 | 216 | } |
85 | 217 |
|
86 | | - /*********************************************************************** |
| 218 | + /*************************************************************************** |
87 | 219 |
|
88 | | - Suppresses log output from the fake node if used version of proto |
89 | | - supports it. |
| 220 | + Scope allocates a request resource acquirer backed by the protected |
| 221 | + `shared_resources`. (Passed as a generic Object to avoid templatising |
| 222 | + this class and others that depend on it.) |
90 | 223 |
|
91 | | - ***********************************************************************/ |
| 224 | + Params: |
| 225 | + handle_request_dg = delegate that receives a resources acquirer and |
| 226 | + initiates handling of a request |
| 227 | +
|
| 228 | + ***************************************************************************/ |
92 | 229 |
|
93 | | - override public void log_errors ( bool log_errors ) |
| 230 | + override protected void getResourceAcquirer ( |
| 231 | + void delegate ( Object resource_acquirer ) handle_request_dg ) |
94 | 232 | { |
95 | | - static if (is(typeof(this.node.log_errors(log_errors)))) |
96 | | - this.node.log_errors(log_errors); |
| 233 | + handle_request_dg(this.shared_resources); |
97 | 234 | } |
98 | 235 | } |
99 | 236 |
|
100 | | -version (UnitTest){} |
101 | | -else |
102 | | -void main() |
| 237 | +/******************************************************************************* |
| 238 | +
|
| 239 | + Legacy protocol connection handler. Required by NodeBase but unused in this |
| 240 | + example. |
| 241 | +
|
| 242 | +*******************************************************************************/ |
| 243 | + |
| 244 | +private class ConnHandler : ConnectionHandlerTemplate!(ICommandCodes) |
103 | 245 | { |
104 | | - auto sandbox = DirectorySandbox.create(); |
105 | | - scope (success) |
106 | | - sandbox.remove(); |
| 246 | + import ocean.net.server.connection.IConnectionHandler; |
| 247 | + |
| 248 | + public this ( void delegate(IConnectionHandler) finaliser, |
| 249 | + ConnectionSetupParams params ) |
| 250 | + { |
| 251 | + super(finaliser, params); |
| 252 | + } |
107 | 253 |
|
108 | | - auto node = new TestNode(); |
109 | | - node.start("127.0.0.1", 10000); |
110 | | - node.genConfigFiles("."); |
| 254 | + override protected void handleCommand () {} |
111 | 255 |
|
112 | | - test!("==")(File.get("turtleNode.nodes"), "127.0.0.1:10000\n"); |
113 | | - test!("==")(File.get("turtleNode.neo.nodes"), "127.0.0.1:10100\n"); |
| 256 | + override protected void handleNone () {} |
114 | 257 | } |
0 commit comments