summaryrefslogtreecommitdiff
path: root/src/test/java/testing/protocols/SlowConnectionProtocolTest.java
blob: cf1751761cd1c8d3627cf9891a001be5ad39952f (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
84
85
86
87
88
89
90
package testing.protocols;

import testing.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

/**
 * JUnit test for Slow Connection simulation.
 */
@DisplayName("Slow Connection Simulation Tests")
public class SlowConnectionProtocolTest {
    private HeadlessSimulationRunner runner;
    
    @BeforeEach
    public void setup() {
        runner = new HeadlessSimulationRunner();
    }
    
    @AfterEach
    public void teardown() {
        runner.shutdown();
    }
    
    @Test
    @DisplayName("Test slow connection simulation")
    public void testSlowConnection() throws Exception {
        SimulationResult result = runner.runSimulation(
            "saved-simulations/slow-connection.dat", 
            5000
        );
        
        // Slow connection should show delayed message delivery
        ProtocolVerifier verifier = new ProtocolVerifier()
            .expectLog("activated")
            .expectLog("Message")
            .expectNoLog("ERROR");
            
        VerificationResult verification = verifier.verify(result.getAllLogs());
        assertTrue(verification.passed(), verification.getFailureMessage());
    }
    
    @Test
    @DisplayName("Test message delays in slow connection")
    public void testMessageDelays() throws Exception {
        SimulationResult result = runner.runSimulation(
            "saved-simulations/slow-connection.dat", 
            6000
        );
        
        // Look for evidence of delays
        var sentMessages = result.findAll("Message sent");
        var receivedMessages = result.findAll("Message received");
        
        if (!sentMessages.isEmpty() && !receivedMessages.isEmpty()) {
            // Calculate average delay
            long totalDelay = 0;
            int delayCount = 0;
            
            for (int i = 0; i < Math.min(sentMessages.size(), receivedMessages.size()); i++) {
                long sentTime = sentMessages.get(i).getTimestamp();
                long receivedTime = receivedMessages.get(i).getTimestamp();
                if (receivedTime > sentTime) {
                    totalDelay += (receivedTime - sentTime);
                    delayCount++;
                }
            }
            
            if (delayCount > 0) {
                long avgDelay = totalDelay / delayCount;
                assertTrue(avgDelay > 0, "Should have message delays in slow connection");
            }
        }
    }
    
    @Test
    @DisplayName("Test connection characteristics")
    public void testConnectionCharacteristics() throws Exception {
        SimulationResult result = runner.runSimulation(
            "saved-simulations/slow-connection.dat", 
            4000
        );
        
        // Check for slow/delay related messages
        boolean hasSlowIndication = result.countLogs("slow|Slow|delay|Delay") > 0;
        boolean hasConnection = result.countLogs("connection|Connection") > 0;
        
        assertTrue(hasSlowIndication || hasConnection || result.getAllLogs().size() > 0, 
                  "Should have some activity indicating slow connection");
    }
}