seq-runtime 5.6.1

Runtime library for the Seq programming language
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
use super::lifecycle::{DEFAULT_STACK_SIZE, parse_stack_size};
use super::spawn::free_stack;
use super::yield_ops::TAIL_CALL_COUNTER;
use super::*;
use crate::stack::{Stack, push};
use crate::value::Value;
use std::sync::atomic::{AtomicU32, Ordering};

#[test]
fn test_spawn_strand() {
    unsafe {
        static COUNTER: AtomicU32 = AtomicU32::new(0);

        extern "C" fn test_entry(_stack: Stack) -> Stack {
            COUNTER.fetch_add(1, Ordering::SeqCst);
            std::ptr::null_mut()
        }

        for _ in 0..100 {
            spawn_strand(test_entry);
        }

        std::thread::sleep(std::time::Duration::from_millis(200));
        assert_eq!(COUNTER.load(Ordering::SeqCst), 100);
    }
}

#[test]
fn test_scheduler_init_idempotent() {
    unsafe {
        // Should be safe to call multiple times
        scheduler_init();
        scheduler_init();
        scheduler_init();
    }
}

#[test]
fn test_free_stack_null() {
    // Freeing null should be a no-op
    free_stack(std::ptr::null_mut());
}

#[test]
fn test_free_stack_valid() {
    unsafe {
        // Create a stack, then free it
        let stack = push(crate::stack::alloc_test_stack(), Value::Int(42));
        free_stack(stack);
        // If we get here without crashing, test passed
    }
}

#[test]
fn test_strand_spawn_with_stack() {
    unsafe {
        static COUNTER: AtomicU32 = AtomicU32::new(0);

        extern "C" fn test_entry(stack: Stack) -> Stack {
            COUNTER.fetch_add(1, Ordering::SeqCst);
            // Return the stack as-is (caller will free it)
            stack
        }

        let initial_stack = push(crate::stack::alloc_test_stack(), Value::Int(99));
        strand_spawn(test_entry, initial_stack);

        std::thread::sleep(std::time::Duration::from_millis(200));
        assert_eq!(COUNTER.load(Ordering::SeqCst), 1);
    }
}

#[test]
fn test_scheduler_shutdown() {
    unsafe {
        scheduler_init();
        scheduler_shutdown();
        // Should not crash
    }
}

#[test]
fn test_many_strands_stress() {
    unsafe {
        static COUNTER: AtomicU32 = AtomicU32::new(0);

        extern "C" fn increment(_stack: Stack) -> Stack {
            COUNTER.fetch_add(1, Ordering::SeqCst);
            std::ptr::null_mut()
        }

        // Reset counter for this test
        COUNTER.store(0, Ordering::SeqCst);

        // Spawn many strands to stress test synchronization
        for _ in 0..1000 {
            strand_spawn(increment, std::ptr::null_mut());
        }

        // Wait for all to complete
        wait_all_strands();

        // Verify all strands executed
        assert_eq!(COUNTER.load(Ordering::SeqCst), 1000);
    }
}

#[test]
fn test_strand_ids_are_unique() {
    unsafe {
        use std::collections::HashSet;

        extern "C" fn noop(_stack: Stack) -> Stack {
            std::ptr::null_mut()
        }

        // Spawn strands and collect their IDs
        let mut ids = Vec::new();
        for _ in 0..100 {
            let id = strand_spawn(noop, std::ptr::null_mut());
            ids.push(id);
        }

        // Wait for completion
        wait_all_strands();

        // Verify all IDs are unique
        let unique_ids: HashSet<_> = ids.iter().collect();
        assert_eq!(unique_ids.len(), 100, "All strand IDs should be unique");

        // Verify all IDs are positive
        assert!(
            ids.iter().all(|&id| id > 0),
            "All strand IDs should be positive"
        );
    }
}

#[test]
fn test_arena_reset_with_strands() {
    unsafe {
        use crate::arena;
        use crate::seqstring::arena_string;

        extern "C" fn create_temp_strings(stack: Stack) -> Stack {
            // Create many temporary arena strings (simulating request parsing)
            for i in 0..100 {
                let temp = arena_string(&format!("temporary string {}", i));
                // Use the string temporarily
                assert!(!temp.as_str().is_empty());
                // String is dropped, but memory stays in arena
            }

            // Arena should have allocated memory
            let stats = arena::arena_stats();
            assert!(stats.allocated_bytes > 0, "Arena should have allocations");

            stack // Return empty stack
        }

        // Reset arena before test
        arena::arena_reset();

        // Spawn strand that creates many temp strings
        strand_spawn(create_temp_strings, std::ptr::null_mut());

        // Wait for strand to complete (which calls free_stack -> arena_reset)
        wait_all_strands();

        // After strand exits, arena should be reset
        let stats_after = arena::arena_stats();
        assert_eq!(
            stats_after.allocated_bytes, 0,
            "Arena should be reset after strand exits"
        );
    }
}

#[test]
fn test_arena_with_channel_send() {
    unsafe {
        use crate::channel::{close_channel, make_channel, receive, send};
        use crate::stack::{pop, push};
        use crate::value::Value;
        use std::sync::Arc;
        use std::sync::atomic::{AtomicI64, AtomicU32, Ordering};

        static RECEIVED_COUNT: AtomicU32 = AtomicU32::new(0);
        static CHANNEL_PTR: AtomicI64 = AtomicI64::new(0);

        // Create channel
        let stack = crate::stack::alloc_test_stack();
        let stack = make_channel(stack);
        let (stack, chan_val) = pop(stack);
        let channel = match chan_val {
            Value::Channel(ch) => ch,
            _ => panic!("Expected Channel"),
        };

        // Store channel pointer for strands
        let ch_ptr = Arc::as_ptr(&channel) as i64;
        CHANNEL_PTR.store(ch_ptr, Ordering::Release);

        // Keep Arc alive
        std::mem::forget(channel.clone());
        std::mem::forget(channel.clone());

        // Sender strand: creates arena string, sends through channel
        extern "C" fn sender(_stack: Stack) -> Stack {
            use crate::seqstring::arena_string;
            use crate::value::ChannelData;
            use std::sync::Arc;

            unsafe {
                let ch_ptr = CHANNEL_PTR.load(Ordering::Acquire) as *const ChannelData;
                let channel = Arc::from_raw(ch_ptr);
                let channel_clone = Arc::clone(&channel);
                std::mem::forget(channel); // Don't drop

                // Create arena string
                let msg = arena_string("Hello from sender!");

                // Push string and channel for send
                let stack = push(crate::stack::alloc_test_stack(), Value::String(msg));
                let stack = push(stack, Value::Channel(channel_clone));

                // Send (will clone to global)
                send(stack)
            }
        }

        // Receiver strand: receives string from channel
        extern "C" fn receiver(_stack: Stack) -> Stack {
            use crate::value::ChannelData;
            use std::sync::Arc;
            use std::sync::atomic::Ordering;

            unsafe {
                let ch_ptr = CHANNEL_PTR.load(Ordering::Acquire) as *const ChannelData;
                let channel = Arc::from_raw(ch_ptr);
                let channel_clone = Arc::clone(&channel);
                std::mem::forget(channel); // Don't drop

                // Push channel for receive
                let stack = push(
                    crate::stack::alloc_test_stack(),
                    Value::Channel(channel_clone),
                );

                // Receive message (returns value, success_flag)
                let stack = receive(stack);

                // Pop success flag first, then message
                let (stack, _success) = pop(stack);
                let (_stack, msg_val) = pop(stack);
                match msg_val {
                    Value::String(s) => {
                        assert_eq!(s.as_str(), "Hello from sender!");
                        RECEIVED_COUNT.fetch_add(1, Ordering::SeqCst);
                    }
                    _ => panic!("Expected String"),
                }

                std::ptr::null_mut()
            }
        }

        // Spawn sender and receiver
        spawn_strand(sender);
        spawn_strand(receiver);

        // Wait for both strands
        wait_all_strands();

        // Verify message was received
        assert_eq!(
            RECEIVED_COUNT.load(Ordering::SeqCst),
            1,
            "Receiver should have received message"
        );

        // Clean up channel
        let stack = push(stack, Value::Channel(channel));
        close_channel(stack);
    }
}

#[test]
fn test_no_memory_leak_over_many_iterations() {
    // PR #11 feedback: Verify 10K+ strand iterations don't cause memory growth
    unsafe {
        use crate::arena;
        use crate::seqstring::arena_string;

        extern "C" fn allocate_strings_and_exit(stack: Stack) -> Stack {
            // Simulate request processing: many temp allocations
            for i in 0..50 {
                let temp = arena_string(&format!("request header {}", i));
                assert!(!temp.as_str().is_empty());
                // Strings dropped here but arena memory stays allocated
            }
            stack
        }

        // Run many iterations to detect leaks
        let iterations = 10_000;

        for i in 0..iterations {
            // Reset arena before each iteration to start fresh
            arena::arena_reset();

            // Spawn strand, let it allocate strings, then exit
            strand_spawn(allocate_strings_and_exit, std::ptr::null_mut());

            // Wait for completion (triggers arena reset)
            wait_all_strands();

            // Every 1000 iterations, verify arena is actually reset
            if i % 1000 == 0 {
                let stats = arena::arena_stats();
                assert_eq!(
                    stats.allocated_bytes, 0,
                    "Arena not reset after iteration {} (leaked {} bytes)",
                    i, stats.allocated_bytes
                );
            }
        }

        // Final verification: arena should be empty
        let final_stats = arena::arena_stats();
        assert_eq!(
            final_stats.allocated_bytes, 0,
            "Arena leaked memory after {} iterations ({} bytes)",
            iterations, final_stats.allocated_bytes
        );

        println!(
            "✓ Memory leak test passed: {} iterations with no growth",
            iterations
        );
    }
}

#[test]
fn test_parse_stack_size_valid() {
    assert_eq!(parse_stack_size(Some("2097152".to_string())), 2097152);
    assert_eq!(parse_stack_size(Some("1".to_string())), 1);
    assert_eq!(parse_stack_size(Some("999999999".to_string())), 999999999);
}

#[test]
fn test_parse_stack_size_none() {
    assert_eq!(parse_stack_size(None), DEFAULT_STACK_SIZE);
}

#[test]
fn test_parse_stack_size_zero() {
    // Zero should fall back to default (with warning printed to stderr)
    assert_eq!(parse_stack_size(Some("0".to_string())), DEFAULT_STACK_SIZE);
}

#[test]
fn test_parse_stack_size_invalid() {
    // Non-numeric should fall back to default (with warning printed to stderr)
    assert_eq!(
        parse_stack_size(Some("invalid".to_string())),
        DEFAULT_STACK_SIZE
    );
    assert_eq!(
        parse_stack_size(Some("-100".to_string())),
        DEFAULT_STACK_SIZE
    );
    assert_eq!(parse_stack_size(Some("".to_string())), DEFAULT_STACK_SIZE);
    assert_eq!(
        parse_stack_size(Some("1.5".to_string())),
        DEFAULT_STACK_SIZE
    );
}

#[test]
#[cfg(feature = "diagnostics")]
fn test_strand_registry_basic() {
    let registry = StrandRegistry::new(10);

    // Register some strands
    assert_eq!(registry.register(1), Some(0)); // First slot
    assert_eq!(registry.register(2), Some(1)); // Second slot
    assert_eq!(registry.register(3), Some(2)); // Third slot

    // Verify active strands
    let active: Vec<_> = registry.active_strands().collect();
    assert_eq!(active.len(), 3);

    // Unregister one
    assert!(registry.unregister(2));
    let active: Vec<_> = registry.active_strands().collect();
    assert_eq!(active.len(), 2);

    // Unregister non-existent should return false
    assert!(!registry.unregister(999));
}

#[test]
#[cfg(feature = "diagnostics")]
fn test_strand_registry_overflow() {
    let registry = StrandRegistry::new(3); // Small capacity

    // Fill it up
    assert!(registry.register(1).is_some());
    assert!(registry.register(2).is_some());
    assert!(registry.register(3).is_some());

    // Next should overflow
    assert!(registry.register(4).is_none());
    assert_eq!(registry.overflow_count.load(Ordering::Relaxed), 1);

    // Another overflow
    assert!(registry.register(5).is_none());
    assert_eq!(registry.overflow_count.load(Ordering::Relaxed), 2);
}

#[test]
#[cfg(feature = "diagnostics")]
fn test_strand_registry_slot_reuse() {
    let registry = StrandRegistry::new(3);

    // Fill it up
    registry.register(1);
    registry.register(2);
    registry.register(3);

    // Unregister middle one
    registry.unregister(2);

    // New registration should reuse the slot
    assert!(registry.register(4).is_some());
    assert_eq!(registry.active_strands().count(), 3);
}

#[test]
#[cfg(feature = "diagnostics")]
fn test_strand_registry_concurrent_stress() {
    use std::sync::Arc;
    use std::thread;

    let registry = Arc::new(StrandRegistry::new(50)); // Moderate capacity

    let handles: Vec<_> = (0..100)
        .map(|i| {
            let reg = Arc::clone(&registry);
            thread::spawn(move || {
                let id = (i + 1) as u64;
                // Register
                let _ = reg.register(id);
                // Brief work
                thread::yield_now();
                // Unregister
                reg.unregister(id);
            })
        })
        .collect();

    for h in handles {
        h.join().unwrap();
    }

    // All slots should be free after all threads complete
    assert_eq!(registry.active_strands().count(), 0);
}

#[test]
fn test_strand_lifecycle_counters() {
    unsafe {
        // Reset counters for isolation (not perfect but helps)
        let initial_spawned = TOTAL_SPAWNED.load(Ordering::Relaxed);
        let initial_completed = TOTAL_COMPLETED.load(Ordering::Relaxed);

        static COUNTER: AtomicU32 = AtomicU32::new(0);

        extern "C" fn simple_work(_stack: Stack) -> Stack {
            COUNTER.fetch_add(1, Ordering::SeqCst);
            std::ptr::null_mut()
        }

        COUNTER.store(0, Ordering::SeqCst);

        // Spawn some strands
        for _ in 0..10 {
            strand_spawn(simple_work, std::ptr::null_mut());
        }

        wait_all_strands();

        // Verify counters incremented
        let final_spawned = TOTAL_SPAWNED.load(Ordering::Relaxed);
        let final_completed = TOTAL_COMPLETED.load(Ordering::Relaxed);

        assert!(
            final_spawned >= initial_spawned + 10,
            "TOTAL_SPAWNED should have increased by at least 10"
        );
        assert!(
            final_completed >= initial_completed + 10,
            "TOTAL_COMPLETED should have increased by at least 10"
        );
        assert_eq!(COUNTER.load(Ordering::SeqCst), 10);
    }
}

// =========================================================================
// Yield Safety Valve Tests
// =========================================================================

#[test]
fn test_maybe_yield_disabled_by_default() {
    // When SEQ_YIELD_INTERVAL is not set (or 0), maybe_yield should be a no-op
    // This test verifies it doesn't panic and returns quickly
    for _ in 0..1000 {
        patch_seq_maybe_yield();
    }
}

#[test]
fn test_tail_call_counter_increments() {
    // Verify the thread-local counter increments correctly
    TAIL_CALL_COUNTER.with(|counter| {
        let initial = counter.get();
        patch_seq_maybe_yield();
        patch_seq_maybe_yield();
        patch_seq_maybe_yield();
        // Counter should have incremented (if threshold > 0) or stayed same (if disabled)
        // Either way, it shouldn't panic
        let _ = counter.get();
        // Reset to avoid affecting other tests
        counter.set(initial);
    });
}

#[test]
fn test_counter_overflow_safety() {
    // Verify wrapping_add prevents overflow panic
    TAIL_CALL_COUNTER.with(|counter| {
        let initial = counter.get();
        // Set counter near max to test overflow behavior
        counter.set(u64::MAX - 1);
        // These calls should not panic due to overflow
        patch_seq_maybe_yield();
        patch_seq_maybe_yield();
        patch_seq_maybe_yield();
        // Reset
        counter.set(initial);
    });
}