Skip to content

Commit 625979a

Browse files
committed
Merge branch 'more-unit-tests' into release
2 parents 181ecc4 + e524d80 commit 625979a

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# sim4da - Simulator for Distributed Algorithms
22

3-
The simulator strives to abstract from most of the details in network programming and to ease the development of algorithm simulations. The simulator core is self-contained in a Java module `dev.oxoo2a.sim4da`. The distributed algorithm simulation is controlled by an instance of class `Simulator`:
3+
The simulator strives to abstract from most of the details in network programming and to ease the development of algorithm simulations. The simulator core is self-contained in a Java module `dev.oxoo2a.sim4da`.
4+
5+
## Writing your own simulation
6+
7+
The distributed algorithm simulation is controlled by an instance of class `Simulator`:
48

59
```Java
610
Simulator s = new Simulator(n_nodes);
@@ -31,3 +35,36 @@ Inside `main`, the following methods are available:
3135
- `sendBroadcast(Message message)`: sending a `HashMap`-like message encoded in JSON to all nodes (except the sender itself)
3236
- `stillSimulating()`: returns `true` while the duration for the simulation is not exceeded
3337
- `receive()`: blocks until a message is received by the node; an object of type `Network.Message` is returned, which stores `sender_id`, `receiver_id`, send mode (unicast or broadcast), and the `payload` as a string. If a `Message` is expected, this payload must be deserialized from JSON back into an object of type `Message` by calling `Message.fromJson(payload)`.
38+
39+
An example implementation for a distributed algorithm simulation is available as a test case (`BroadcastNode.java` and `SimulatorTest.java`).
40+
41+
## Building the sim4da jar
42+
43+
`Gradle` is used as the build tool. `gradle build` and `gradle shadowJar` within the root directory of the repository generate the required jar files. The fat jar contains the transitive dependencies to `gson`, `junit` and `log4j2`.
44+
45+
## Logging
46+
47+
The simulator core can create trace files of every simulation thanks to the logging framework `log4j2`. A default configuration file is part of the simulator resources. The default logging configuration is similar to:
48+
49+
```xml
50+
<?xml version="1.0" encoding="UTF-8"?>
51+
<Configuration status="WARN">
52+
<Appenders>
53+
<Console name="console" target="SYSTEM_OUT">
54+
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
55+
</Console>
56+
57+
<File name="logfile" fileName="sim4da-app.log" append="false">
58+
<PatternLayout pattern="%msg%n"/>
59+
</File>
60+
</Appenders>
61+
<Loggers>
62+
<Logger name="sim4da" level="trace" additivity="false">
63+
<AppenderRef ref="logfile"/>
64+
</Logger>
65+
<Root level="warn">
66+
<AppenderRef ref="console"/>
67+
</Root>
68+
</Loggers>
69+
</Configuration>
70+
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies {
2929
}
3030

3131
group = 'dev.oxoo2a'
32-
version = '1.0-SNAPSHOT'
32+
version = '1.4-SNAPSHOT'
3333
description = 'sim4da'
3434
java.sourceCompatibility = JavaVersion.VERSION_17
3535

src/test/java/dev/oxoo2a/sim4da/MessageTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.BeforeAll;
56

67
public class MessageTest {
8+
@BeforeAll
9+
public static void createMessage () {
10+
m = new Message().add("a","b").add("c","d");
11+
}
12+
13+
private static Message m;
714

815
@Test
916
public void testMessageBasics () {
10-
Message m = new Message().add("a","b");
1117
assertEquals(m.query("a"), "b");
18+
assertEquals(m.query("c"), "d");
19+
assertEquals(m.query("no_key"),null);
20+
}
21+
22+
@Test
23+
public void serializeAndDeserialize () {
24+
String m_json = m.toJson();
25+
Message m2 = Message.fromJson(m_json);
26+
assertEquals(m2.query("a"),"b");
27+
assertEquals(m2.query("c"),"d");
1228
}
1329
}

src/test/java/dev/oxoo2a/sim4da/SimulatorTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package dev.oxoo2a.sim4da;
22

33
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.beans.Transient;
6+
47
import org.junit.jupiter.api.Test;
58

69
public class SimulatorTest {
10+
private final int n_nodes = 3;
11+
private final int duration = 2;
712

813
@Test
914
public void simpleSimulation() {
10-
int n_nodes = 3;
11-
int duration = 1;
12-
1315
Simulator s = Simulator.createDefaultSimulator(n_nodes);
1416
for (int id=0; id<n_nodes; id++) {
1517
Node n = new BroadcastNode(id);
@@ -22,4 +24,12 @@ public void simpleSimulation() {
2224
fail("Not all nodes instantiated");
2325
}
2426
}
27+
28+
@Test
29+
public void someNodesNotInstantiated () {
30+
Simulator s = Simulator.createDefaultSimulator(n_nodes);
31+
s.attachNode(0,new BroadcastNode(0));
32+
s.attachNode(1,new BroadcastNode(1));
33+
assertThrows(InstantiationException.class,() -> {s.runSimulation(duration);});
34+
}
2535
}

0 commit comments

Comments
 (0)