Skip to content

Commit 62c2478

Browse files
author
Gavin Norman
committed
Adapt the turtle env node test to use the new TestNode
1 parent 357a743 commit 62c2478

File tree

1 file changed

+188
-56
lines changed
  • integrationtest/turtleenv

1 file changed

+188
-56
lines changed

integrationtest/turtleenv/main.d

Lines changed: 188 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************************
22
3-
Test of the abstract turtle node extension.
3+
Test of the turtle swarm node extension.
44
55
Copyright:
66
Copyright (c) 2018 dunnhumby Germany GmbH. All rights reserved.
@@ -12,103 +12,235 @@
1212

1313
module integrationtest.turtleenv.main;
1414

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;
1718
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;
2022

21-
/// Node used to the turtle test
22-
private class TurtleNode
23+
version (UnitTest){}
24+
else
25+
void main()
2326
{
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();
2730

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();
3438
}
3539

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
3847
{
39-
/***********************************************************************
48+
import integrationtest.neo.client.Client;
49+
import ocean.core.Test;
50+
import ocean.io.device.File;
4051

41-
Creates a fake node at the specified address/port.
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;
57+
58+
/***************************************************************************
59+
60+
Constructor.
4261
4362
Params:
44-
node_item = address/port
63+
node = node to test
4564
46-
***********************************************************************/
65+
***************************************************************************/
4766

48-
override protected TurtleNode createNode ( AddrPort addrport )
67+
public this ( MyNode node )
4968
{
50-
return new TurtleNode(addrport);
69+
this.node = node;
5170
}
5271

53-
/***********************************************************************
72+
/***************************************************************************
73+
74+
Task entry point. Runs a series of tests on the node then shuts down the
75+
scheduler.
76+
77+
***************************************************************************/
78+
79+
public override void run ( )
80+
{
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();
108+
}
109+
110+
/***************************************************************************
111+
112+
Uses the client to put a record to the node, then read it back.
54113
55114
Returns:
56-
address/port on which node is listening
115+
true if everything succeeded, false on error
57116
58-
***********************************************************************/
117+
***************************************************************************/
59118

60-
override public AddrPort node_addrport ( )
119+
private bool talkToNode ( )
61120
{
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 || value != "hello" )
130+
return false;
131+
132+
return true;
64133
}
65134

66-
/***********************************************************************
135+
/***************************************************************************
67136
68-
Fake node service stop implementation.
137+
Dummy connection notifier. Required by the client, but unused.
69138
70-
***********************************************************************/
139+
***************************************************************************/
71140

72-
protected override void stopImpl ( )
141+
private void connNotifier ( Client.Neo.ConnNotification info )
73142
{
74143
}
144+
}
75145

76-
/***********************************************************************
146+
/*******************************************************************************
77147
78-
Removes all data from the fake node service.
148+
Test node implementing the protocol defined in integrationtest.neo.node.
79149
80-
***********************************************************************/
150+
*******************************************************************************/
151+
152+
public class MyNode : TestNode!(ConnHandler)
153+
{
154+
import swarm.neo.AddrPort;
81155

82-
override public void clear ( )
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 )
83173
{
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);
84193
}
85194

86-
/***********************************************************************
195+
/***************************************************************************
196+
197+
Returns:
198+
identifier string for this node
87199
88-
Suppresses log output from the fake node if used version of proto
89-
supports it.
200+
***************************************************************************/
201+
202+
protected override cstring id ( )
203+
{
204+
return "testnode";
205+
}
206+
207+
/***************************************************************************
208+
209+
Scope allocates a request resource acquirer backed by the protected
210+
`shared_resources`. (Passed as a generic Object to avoid templatising
211+
this class and others that depend on it.)
212+
213+
Params:
214+
handle_request_dg = delegate that receives a resources acquirer and
215+
initiates handling of a request
90216
91-
***********************************************************************/
217+
***************************************************************************/
92218

93-
override public void log_errors ( bool log_errors )
219+
override protected void getResourceAcquirer (
220+
void delegate ( Object resource_acquirer ) handle_request_dg )
94221
{
95-
static if (is(typeof(this.node.log_errors(log_errors))))
96-
this.node.log_errors(log_errors);
222+
handle_request_dg(this.shared_resources);
97223
}
98224
}
99225

100-
version (UnitTest){}
101-
else
102-
void main()
226+
/*******************************************************************************
227+
228+
Legacy protocol connection handler. Required by NodeBase but unused in this
229+
example.
230+
231+
*******************************************************************************/
232+
233+
private class ConnHandler : ConnectionHandlerTemplate!(ICommandCodes)
103234
{
104-
auto sandbox = DirectorySandbox.create();
105-
scope (success)
106-
sandbox.remove();
235+
import ocean.net.server.connection.IConnectionHandler;
236+
237+
public this ( void delegate(IConnectionHandler) finaliser,
238+
ConnectionSetupParams params )
239+
{
240+
super(finaliser, params);
241+
}
107242

108-
auto node = new TestNode();
109-
node.start("127.0.0.1", 10000);
110-
node.genConfigFiles(".");
243+
override protected void handleCommand () {}
111244

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");
245+
override protected void handleNone () {}
114246
}

0 commit comments

Comments
 (0)