rukko 0.1.0

A Rust library for communicating with JVM-based Pekko actors
Documentation
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Comprehensive test suite for Rukko
//! 
//! This module contains extensive tests to ensure the reliability of
//! the Pekko Artery protocol implementation.

use crate::actor::ActorSystem;
use crate::error::RukkoError;
use crate::protocol::{ActorPath, FrameHeader, Message, MessageEnvelope, StreamId, UniqueAddress, InternalMessage};
use crate::transport::ArteryTransport;
use std::time::Duration;
use tokio::time::timeout;

/// Test module for protocol edge cases and error handling
#[cfg(test)]
mod protocol_edge_cases {
    use super::*;
    
    #[test]
    fn test_invalid_actor_paths() {
        // Missing protocol
        assert!(ActorPath::from_string("invalid_path".to_string()).is_err());
        
        // Missing @
        assert!(ActorPath::from_string("pekko://system/user/actor".to_string()).is_err());
        
        // Missing port
        assert!(ActorPath::from_string("pekko://system@host/user/actor".to_string()).is_err());
        
        // Invalid port
        assert!(ActorPath::from_string("pekko://system@host:invalid/user/actor".to_string()).is_err());
        
        // Missing path
        assert!(ActorPath::from_string("pekko://system@host:1234".to_string()).is_err());
    }

    #[test]
    fn test_pekko_protocol_support() {
        let path_str = "pekko://TestSystem@127.0.0.1:25552/user/testActor";
        let path = ActorPath::from_string(path_str.to_string()).unwrap();
        
        assert_eq!(path.system, "TestSystem");
        assert_eq!(path.host, "127.0.0.1");
        assert_eq!(path.port, 25552);
        assert_eq!(path.path, "user/testActor");
    }

    #[test]
    fn test_message_types() {
        // Text message
        let text_msg = Message::text("Hello");
        // Note: serializer_id is now internal to InternalMessage
        if let Some(content) = text_msg.as_text() {
            assert_eq!(content, "Hello");
        } else {
            panic!("Expected text message");
        }
    }

    #[test]
    fn test_handshake_messages() {
        let from_addr = ActorPath::new(
            "TestSystem".to_string(),
            "127.0.0.1".to_string(),
            12345,
            "system".to_string(),
        );
        let to_addr = ActorPath::new(
            "TargetSystem".to_string(),
            "127.0.0.1".to_string(),
            25552,
            "system".to_string(),
        );
        let unique_addr = UniqueAddress {
            address: from_addr.clone(),
            uid: 123456789,
        };

        // Note: handshake messages are now internal and not exposed to users
        let handshake_req = InternalMessage::handshake_req(unique_addr.clone(), to_addr.clone());
        assert_eq!(handshake_req.serializer_id, 17);

        let handshake_rsp = InternalMessage::handshake_rsp(unique_addr);
        assert_eq!(handshake_rsp.serializer_id, 17);
    }

    #[test]
    fn test_stream_id_conversion() {
        assert_eq!(StreamId::from_u8(0x01).unwrap(), StreamId::Control);
        assert_eq!(StreamId::from_u8(0x02).unwrap(), StreamId::Ordinary);
        assert_eq!(StreamId::from_u8(0x03).unwrap(), StreamId::Large);
        
        assert!(StreamId::from_u8(0x04).is_err());
        assert!(StreamId::from_u8(0x00).is_err());
    }

    #[test]
    fn test_frame_header_encoding_decoding() {
        let header = FrameHeader::new(12345);
        let encoded = header.encode();
        
        // Should be 4 bytes, little endian
        assert_eq!(encoded.len(), 4);
        assert_eq!(encoded, &12345u32.to_le_bytes()[..]);
        
        let mut buf = encoded;
        let decoded = FrameHeader::decode(&mut buf).unwrap();
        assert_eq!(decoded.size, 12345);
    }

    #[test]
    fn test_message_envelope_creation() {
        let sender = ActorPath::new(
            "SenderSystem".to_string(),
            "127.0.0.1".to_string(),
            12345,
            "user/sender".to_string(),
        );
        let recipient = ActorPath::new(
            "RecipientSystem".to_string(),
            "127.0.0.1".to_string(),
            25552,
            "user/recipient".to_string(),
        );
        let message = Message::text("Test message");
        
        let internal_message = InternalMessage::from_user_message(&message);
        let envelope = MessageEnvelope::new(sender.clone(), recipient.clone(), internal_message).unwrap();
        
        assert_eq!(envelope.version, 0);
        assert_eq!(envelope.flags, 0);
        assert_eq!(envelope.serializer_id, 20);
        assert_eq!(envelope.sender, sender);
        assert_eq!(envelope.recipient, recipient);
        assert_eq!(envelope.class_manifest, "");
    }

    #[test]
    fn test_message_envelope_encoding() {
        let sender = ActorPath::new(
            "SenderSystem".to_string(),
            "127.0.0.1".to_string(),
            12345,
            "user/sender".to_string(),
        );
        let recipient = ActorPath::new(
            "RecipientSystem".to_string(),
            "127.0.0.1".to_string(),
            25552,
            "user/recipient".to_string(),
        );
        let message = Message::text("Test");
        
        let internal_message = InternalMessage::from_user_message(&message);
        let mut envelope = MessageEnvelope::new(sender, recipient, internal_message).unwrap();
        envelope.uid = 987654321;
        
        let encoded = envelope.encode().unwrap();
        
        // Should have at least the fixed header (28 bytes) plus literals
        assert!(encoded.len() > 28);
        
        // Check version at offset 0
        assert_eq!(encoded[0], 0);
        
        // Check UID at offset 4-11 (little endian)
        let uid_bytes = &encoded[4..12];
        let decoded_uid = u64::from_le_bytes([
            uid_bytes[0], uid_bytes[1], uid_bytes[2], uid_bytes[3],
            uid_bytes[4], uid_bytes[5], uid_bytes[6], uid_bytes[7],
        ]);
        assert_eq!(decoded_uid, 987654321);
    }

    #[test]
    fn test_message_envelope_decode_roundtrip() {
        let sender = ActorPath::new(
            "SenderSystem".to_string(),
            "127.0.0.1".to_string(),
            12345,
            "user/sender".to_string(),
        );
        let recipient = ActorPath::new(
            "RecipientSystem".to_string(),
            "127.0.0.1".to_string(),
            25552,
            "user/recipient".to_string(),
        );
        let message = Message::text("Test message");
        
        let internal_message = InternalMessage::from_user_message(&message);
        let mut original_envelope = MessageEnvelope::new(sender.clone(), recipient.clone(), internal_message).unwrap();
        original_envelope.uid = 123456789;
        
        let encoded = original_envelope.encode().unwrap();
        let mut buf = encoded;
        let decoded_envelope = MessageEnvelope::decode(&mut buf).unwrap();
        
        assert_eq!(decoded_envelope.version, original_envelope.version);
        assert_eq!(decoded_envelope.uid, original_envelope.uid);
        assert_eq!(decoded_envelope.serializer_id, original_envelope.serializer_id);
        assert_eq!(decoded_envelope.sender, original_envelope.sender);
        assert_eq!(decoded_envelope.recipient, original_envelope.recipient);
        assert_eq!(decoded_envelope.class_manifest, original_envelope.class_manifest);
    }
}

/// Test module for actor system functionality
#[cfg(test)]
mod actor_system_tests {
    use super::*;

    #[tokio::test]
    async fn test_actor_system_creation_with_unique_ports() {
        let system1 = ActorSystem::new("TestSystem1").await.unwrap();
        let system2 = ActorSystem::new("TestSystem2").await.unwrap();
        
        // Should have different UIDs
        assert_ne!(system1.uid(), system2.uid());
        
        // Should have different ports (OS-allocated)
        assert_ne!(system1.bound_port(), system2.bound_port());
        
        // Should have unique addresses
        assert_ne!(system1.uid(), system2.uid());
        assert_ne!(system1.path().port, system2.path().port);
        
        system1.shutdown().await;
        system2.shutdown().await;
    }

    #[tokio::test]
    async fn test_actor_selection_parsing() {
        let system = ActorSystem::new("TestSystem").await.unwrap();
        
        // Test various address formats
        let valid_addresses = vec![
            "pekko://RemoteSystem@127.0.0.1:25552/user/testActor",
            "pekko://RemoteSystem@127.0.0.1:25552/user/testActor",
            "pekko://System@localhost:8080/user/service/worker",
        ];
        
        for address in valid_addresses {
            // This will fail to connect but should parse correctly
            match system.actor_selection(address).await {
                Ok(selection) => {
                    assert!(!selection.path().system.is_empty());
                    assert!(!selection.path().host.is_empty());
                    assert!(selection.path().port > 0);
                }
                Err(RukkoError::Connection(_)) => {
                    // Expected - no server running
                }
                Err(e) => panic!("Unexpected error for {}: {}", address, e),
            }
        }
        
        system.shutdown().await;
    }

    #[tokio::test]
    async fn test_direct_system_methods() -> Result<(), Box<dyn std::error::Error>> {
        let system = ActorSystem::new("TestSystem").await.unwrap();
        
        let test_address = "pekko://NonExistent@127.0.0.1:65533/user/test";
        let message = Message::text("Test message");
        
        // Should fail to connect but not panic
        let selection = system.actor_selection(test_address).await?;
        
        // tell() is fire-and-forget, connection issues happen at transport level
        // We just verify it doesn't panic
        selection.tell(message);
        
        system.shutdown().await;
        Ok(())
    }

}

/// Test module for transport layer functionality
#[cfg(test)]
mod transport_tests {
    use super::*;
    use tokio::net::TcpListener;

    #[tokio::test]
    async fn test_transport_creation() {
        let _transport1 = ArteryTransport::new(123456, 8080, "127.0.0.1".to_string(), "TestSystem1".to_string());
        let _transport2 = ArteryTransport::new(789012, 9090, "192.168.1.100".to_string(), "TestSystem2".to_string());
        
        // Basic construction should work without panicking
        // Sequence counter is private, so we can't test it directly
    }

    #[tokio::test]
    async fn test_connection_to_nonexistent_server() -> Result<(), Box<dyn std::error::Error>> {
        let transport = ArteryTransport::new(456789, 25552, "127.0.0.1".to_string(), "ConnTestSystem".to_string());
        
        // Should fail to connect to non-existent server
        let result = transport.connect("127.0.0.1", "TestSystem", 65535).await;
        assert!(result.is_err());
        
        match result {
            Err(RukkoError::Connection(_)) => {
                // Expected
            }
            Err(e) => panic!("Unexpected error type: {}", e),
            Ok(_) => panic!("Should not succeed connecting to non-existent server"),
        }
        Ok(())
    }

    #[test]
    fn test_transport_basic_functionality() {
        let _transport = ArteryTransport::new(111222, 2552, "127.0.0.1".to_string(), "BasicTestSystem".to_string());
        // Basic test that transport can be created without errors
        // Sequence counter functionality is tested indirectly through integration tests
    }

    #[tokio::test]
    async fn test_tcp_server_startup() {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let port = listener.local_addr().unwrap().port();
        
        let transport = std::sync::Arc::new(ArteryTransport::new(123456, port, "127.0.0.1".to_string(), "TestTcpServer".to_string()));
        
        // Start server in background
        let transport_clone = transport.clone();
        let server_handle = tokio::spawn(async move {
            // Run server for a short time, then exit
            let server_future = ArteryTransport::run_server(listener, transport_clone);
            timeout(Duration::from_millis(100), server_future).await
        });
        
        // Give it a moment to start
        tokio::time::sleep(Duration::from_millis(10)).await;
        
        // Try to connect (will fail handshake but should accept connection)
        let connect_result = tokio::net::TcpStream::connect(format!("127.0.0.1:{}", port)).await;
        assert!(connect_result.is_ok());
        
        // Clean up
        server_handle.abort();
    }
}

/// Test module for error handling
#[cfg(test)]
mod error_tests {
    use super::*;

    #[test]
    fn test_error_types() {
        let io_error = std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "test");
        let pekko_error = RukkoError::Connection(io_error);
        
        assert!(matches!(pekko_error, RukkoError::Connection(_)));
        assert!(pekko_error.to_string().contains("Connection error"));
        
        let protocol_error = RukkoError::Protocol("Invalid format".to_string());
        assert!(protocol_error.to_string().contains("Protocol error"));
        
        let timeout_error = RukkoError::Timeout;
        assert!(timeout_error.to_string().contains("Timeout"));
    }

    #[test]
    fn test_error_conversion() {
        let io_error = std::io::Error::new(std::io::ErrorKind::TimedOut, "timeout");
        let pekko_error: RukkoError = io_error.into();
        
        assert!(matches!(pekko_error, RukkoError::Connection(_)));
    }
}

/// Test module for serialization edge cases
#[cfg(test)]
mod serialization_tests {
    use super::*;

    #[test]
    fn test_text_edge_cases() {
        // Empty text
        let msg = Message::text("");
        let encoded = InternalMessage::encode_user_message(&msg).unwrap();
        assert_eq!(encoded.as_ref(), b"");
        
        // Text with special characters
        let special_chars = "Hello\nWorld\t\"";
        let msg = Message::text(special_chars);
        let encoded = InternalMessage::encode_user_message(&msg).unwrap();
        assert_eq!(encoded.as_ref(), special_chars.as_bytes());
        
        // Large text
        let large_text = "x".repeat(10000);
        let msg = Message::text(&large_text);
        let encoded = InternalMessage::encode_user_message(&msg).unwrap();
        assert_eq!(encoded.as_ref(), large_text.as_bytes());
    }

}

/// Integration tests with timeouts
#[cfg(test)]
mod integration_tests {
    use super::*;

    #[tokio::test]
    async fn test_ask_timeout_behavior() -> Result<(), Box<dyn std::error::Error>> {
        let system = ActorSystem::new("TestSystem").await.unwrap();
        
        // Try to ask a non-existent actor - should timeout
        let non_existent = "pekko://NonExistent@127.0.0.1:65534/user/test";
        let message = Message::text("Hello");
        
        let start_time = std::time::Instant::now();
        let selection = system.actor_selection(non_existent).await?;
        let result = timeout(Duration::from_millis(200), selection.ask(message)).await;
        let elapsed = start_time.elapsed();
        
        // Should timeout quickly (connection failure) or within our timeout
        assert!(elapsed < Duration::from_millis(300));
        
        match result {
            Err(_) => {
                // Timeout from our wrapper - acceptable
            }
            Ok(Err(RukkoError::Connection(_))) => {
                // Connection error - also acceptable
            }
            Ok(Err(RukkoError::Timeout)) => {
                // Pekko timeout - also acceptable
            }
            Ok(Ok(_)) => panic!("Should not succeed"),
            Ok(Err(e)) => panic!("Unexpected error: {}", e),
        }
        
        system.shutdown().await;
        Ok(())
    }

    #[tokio::test]
    async fn test_multiple_concurrent_connections() {
        let system = ActorSystem::new("TestSystem").await.unwrap();
        
        // Try multiple concurrent connections
        let mut handles = Vec::new();
        
        for i in 0..5 {
            let system_clone = system.clone();
            let handle = tokio::spawn(async move {
                let address = format!("pekko://Test{}@127.0.0.1:6553{}/user/test", i, i);
                let message = Message::text(&format!("Message {}", i));
                
                // This will fail but should handle multiple concurrent attempts gracefully
                if let Ok(selection) = system_clone.actor_selection(&address).await {
                    let _result = timeout(Duration::from_millis(50), async { selection.tell(message); Ok::<(), Box<dyn std::error::Error>>(()) }).await;
                }
            });
            handles.push(handle);
        }
        
        // Wait for all to complete
        for handle in handles {
            handle.await.unwrap();
        }
        
        system.shutdown().await;
    }
}