blob: abe9a5d7744d6b84abb8a81b1e4066e96f3e9552 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
package testing.protocols;
import testing.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
/**
* JUnit tests for commit protocols (one-phase and two-phase).
*/
@DisplayName("Commit Protocol Tests")
public class CommitProtocolTest {
private HeadlessSimulationRunner runner;
@BeforeEach
public void setup() {
runner = new HeadlessSimulationRunner();
}
@AfterEach
public void teardown() {
runner.shutdown();
}
@Test
@DisplayName("Test One-Phase Commit protocol")
public void testOnePhaseCommit() throws Exception {
SimulationResult result = runner.runSimulation(
"saved-simulations/one-phase-commit.dat",
3000
);
ProtocolVerifier verifier = new ProtocolVerifier()
.expectLog("1-Phase Commit.*activated|One.*Phase.*activated")
.expectLog("commit|Commit|COMMIT")
.expectLog("Message")
.expectNoLog("ERROR")
.expectNoLog("abort|ABORT");
VerificationResult verification = verifier.verify(result.getAllLogs());
assertTrue(verification.passed(), verification.getFailureMessage());
}
@Test
@DisplayName("Test Two-Phase Commit protocol")
public void testTwoPhaseCommit() throws Exception {
SimulationResult result = runner.runSimulation(
"saved-simulations/two-phase-commit.dat",
4000
);
ProtocolVerifier verifier = new ProtocolVerifier()
.expectLog("2-Phase Commit.*activated|Two.*Phase.*activated")
.expectLog("Message")
.expectNoLog("ERROR");
VerificationResult verification = verifier.verify(result.getAllLogs());
assertTrue(verification.passed(), verification.getFailureMessage());
// Two-phase commit should have prepare/vote and commit phases
boolean hasPrepare = result.countLogs("prepare|Prepare|PREPARE|vote|Vote") > 0;
boolean hasCommit = result.countLogs("commit|Commit|COMMIT") > 0;
assertTrue(hasPrepare || hasCommit,
"Should have prepare/vote or commit messages");
}
@Test
@DisplayName("Test commit protocol coordinator behavior")
public void testCoordinatorBehavior() throws Exception {
SimulationResult result = runner.runSimulation(
"saved-simulations/two-phase-commit.dat",
5000
);
// Look for coordinator-specific messages
boolean hasCoordinator = result.countLogs("coordinator|Coordinator|COORDINATOR") > 0;
boolean hasParticipant = result.countLogs("participant|Participant|PARTICIPANT") > 0;
boolean hasTransaction = result.countLogs("transaction|Transaction") > 0;
assertTrue(hasCoordinator || hasParticipant || hasTransaction,
"Should have coordinator/participant activity");
}
}
|