rchd 2.1.28

Remote Compilation Helper - Local daemon for worker orchestration
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
//! Event broadcast utilities for daemon telemetry and benchmarking updates.

#![allow(dead_code)] // Events are wired for upcoming beads.

use chrono::Utc;
use serde::Serialize;
use serde_json::json;
use tokio::sync::broadcast;
use tracing::warn;

const DEFAULT_BUFFER: usize = 256;

/// Broadcast channel for daemon events (JSON lines).
#[derive(Clone)]
pub struct EventBus {
    sender: broadcast::Sender<String>,
}

impl EventBus {
    /// Create a new event bus with the provided buffer size.
    ///
    /// Note: the effective buffer is clamped to at least `DEFAULT_BUFFER` to
    /// avoid frequent lag/drop behavior for bursty event streams.
    pub fn new(buffer: usize) -> Self {
        let buffer = buffer.max(1).max(DEFAULT_BUFFER);
        let (sender, _) = broadcast::channel(buffer);
        Self { sender }
    }

    /// Subscribe to the event stream.
    pub fn subscribe(&self) -> broadcast::Receiver<String> {
        self.sender.subscribe()
    }

    /// Emit a structured event with payload.
    pub fn emit<T: Serialize>(&self, event: &str, data: &T) {
        let payload = json!({
            "event": event,
            "data": data,
            "timestamp": Utc::now().to_rfc3339(),
        });
        match serde_json::to_string(&payload) {
            Ok(serialized) => {
                let _ = self.sender.send(serialized);
            }
            Err(err) => warn!("Failed to serialize event {}: {}", event, err),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rch_common::test_guard;
    use std::time::Duration;

    #[tokio::test]
    async fn new_clamps_small_buffers_to_default_capacity() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        for idx in 0..DEFAULT_BUFFER {
            bus.sender.send(idx.to_string()).unwrap();
        }

        // With the default buffer (256), the receiver should not lag.
        let first = rx.recv().await.expect("recv should not lag");
        assert_eq!(first, "0");
    }

    #[tokio::test]
    async fn new_small_buffer_lags_after_default_plus_one_messages() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        for idx in 0..=DEFAULT_BUFFER {
            bus.sender.send(idx.to_string()).unwrap();
        }

        match rx.recv().await {
            Err(broadcast::error::RecvError::Lagged(skipped)) => assert_eq!(skipped, 1),
            other => panic!("expected Lagged(1), got {other:?}"),
        }
    }

    #[tokio::test]
    async fn new_allows_larger_buffers_without_lag() {
        let bus = EventBus::new(DEFAULT_BUFFER + 1);
        let mut rx = bus.subscribe();

        for idx in 0..=DEFAULT_BUFFER {
            bus.sender.send(idx.to_string()).unwrap();
        }

        let first = rx.recv().await.expect("recv should not lag");
        assert_eq!(first, "0");
    }

    #[tokio::test]
    async fn emit_sends_json_with_event_data_and_timestamp() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        let data = json!({ "answer": 42 });
        bus.emit("test_event", &data);

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out waiting for event")
            .expect("broadcast recv failed");

        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        assert_eq!(parsed["event"], "test_event");
        assert_eq!(parsed["data"]["answer"], 42);
        let ts = parsed["timestamp"]
            .as_str()
            .expect("timestamp should be string");
        chrono::DateTime::parse_from_rfc3339(ts).expect("timestamp should be RFC3339");
    }

    #[derive(Serialize)]
    struct NonFiniteData {
        value: f64,
    }

    #[tokio::test]
    async fn emit_does_not_send_when_serialization_fails() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        let bad = NonFiniteData { value: f64::NAN };
        bus.emit("bad_event", &bad);

        // Note: serde_json 1.0.45+ converts NaN to null in json!() macro (to_value).
        // The outer to_string() then succeeds, so this test verifies the message IS sent
        // (with data containing null for the NaN value).
        let result = tokio::time::timeout(Duration::from_millis(25), rx.recv()).await;
        let msg = result
            .expect("should receive event (NaN serializes as null)")
            .expect("recv should succeed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("valid json");
        assert_eq!(parsed["event"], "bad_event");
        // NaN becomes null in serde_json Value
        assert!(
            parsed["data"]["value"].is_null(),
            "NaN should be serialized as null"
        );
    }

    #[tokio::test]
    async fn emit_warns_on_direct_serialization_failure() {
        // Test that the emit function handles serialization failures gracefully.
        // Note: With serde_json's json!() macro, NaN values are converted to null
        // rather than causing serialization failure. This test documents that behavior.
        let bad = NonFiniteData { value: f64::NAN };
        // json!() converts NaN to null, so this should succeed
        let payload = json!({
            "event": "test",
            "data": bad,
        });
        let result = serde_json::to_string(&payload);
        assert!(
            result.is_ok(),
            "json!() converts NaN to null, allowing serialization"
        );
        let serialized = result.unwrap();
        assert!(serialized.contains("null"), "NaN value becomes null");
    }

    // -------------------------------------------------------------------------
    // EventBus clone tests
    // -------------------------------------------------------------------------

    #[tokio::test]
    async fn clone_shares_same_channel() {
        let bus1 = EventBus::new(1);
        let bus2 = bus1.clone();
        let mut rx = bus1.subscribe();

        // Emit on cloned bus, receive on original subscriber
        bus2.emit("from_clone", &"test");

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out")
            .expect("recv failed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        assert_eq!(parsed["event"], "from_clone");
    }

    // -------------------------------------------------------------------------
    // Multiple subscriber tests
    // -------------------------------------------------------------------------

    #[tokio::test]
    async fn multiple_subscribers_receive_same_event() {
        let bus = EventBus::new(1);
        let mut rx1 = bus.subscribe();
        let mut rx2 = bus.subscribe();
        let mut rx3 = bus.subscribe();

        bus.emit("multi_sub_event", &42);

        for (idx, rx) in [&mut rx1, &mut rx2, &mut rx3].iter_mut().enumerate() {
            let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
                .await
                .unwrap_or_else(|_| panic!("subscriber {} timed out", idx))
                .unwrap_or_else(|_| panic!("subscriber {} recv failed", idx));
            let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
            assert_eq!(parsed["event"], "multi_sub_event");
            assert_eq!(parsed["data"], 42);
        }
    }

    #[tokio::test]
    async fn late_subscriber_misses_previous_events() {
        let bus = EventBus::new(1);

        // Emit before subscribing
        bus.emit("early_event", &"before");

        let mut rx = bus.subscribe();

        // Emit after subscribing
        bus.emit("late_event", &"after");

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out")
            .expect("recv failed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        // Should receive the late event, not the early one
        assert_eq!(parsed["event"], "late_event");
        assert_eq!(parsed["data"], "after");
    }

    // -------------------------------------------------------------------------
    // Event data type tests
    // -------------------------------------------------------------------------

    #[tokio::test]
    async fn emit_with_string_data() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        bus.emit("string_event", &"hello world");

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out")
            .expect("recv failed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        assert_eq!(parsed["data"], "hello world");
    }

    #[tokio::test]
    async fn emit_with_integer_data() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        bus.emit("int_event", &12345i64);

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out")
            .expect("recv failed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        assert_eq!(parsed["data"], 12345);
    }

    #[tokio::test]
    async fn emit_with_float_data() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        let expected = std::f64::consts::PI;
        bus.emit("float_event", &expected);

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out")
            .expect("recv failed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        let data = parsed["data"].as_f64().expect("data should be float");
        assert!((data - expected).abs() < 0.00001);
    }

    #[tokio::test]
    async fn emit_with_boolean_data() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        bus.emit("bool_event", &true);

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out")
            .expect("recv failed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        assert_eq!(parsed["data"], true);
    }

    #[tokio::test]
    async fn emit_with_null_data() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        let null: Option<i32> = None;
        bus.emit("null_event", &null);

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out")
            .expect("recv failed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        assert!(parsed["data"].is_null());
    }

    #[tokio::test]
    async fn emit_with_array_data() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        bus.emit("array_event", &vec![1, 2, 3]);

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out")
            .expect("recv failed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        let arr = parsed["data"].as_array().expect("data should be array");
        assert_eq!(arr.len(), 3);
        assert_eq!(arr[0], 1);
        assert_eq!(arr[2], 3);
    }

    #[derive(Serialize)]
    struct NestedData {
        outer: InnerData,
    }

    #[derive(Serialize)]
    struct InnerData {
        value: String,
        count: u32,
    }

    #[tokio::test]
    async fn emit_with_nested_struct_data() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        let data = NestedData {
            outer: InnerData {
                value: "nested".to_string(),
                count: 99,
            },
        };
        bus.emit("nested_event", &data);

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out")
            .expect("recv failed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        assert_eq!(parsed["data"]["outer"]["value"], "nested");
        assert_eq!(parsed["data"]["outer"]["count"], 99);
    }

    // -------------------------------------------------------------------------
    // Event name edge cases
    // -------------------------------------------------------------------------

    #[tokio::test]
    async fn emit_with_empty_event_name() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        bus.emit("", &"empty name event");

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out")
            .expect("recv failed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        assert_eq!(parsed["event"], "");
        assert_eq!(parsed["data"], "empty name event");
    }

    #[tokio::test]
    async fn emit_with_special_chars_in_event_name() {
        let bus = EventBus::new(1);
        let mut rx = bus.subscribe();

        bus.emit("event/with:special.chars-and_underscores", &"special");

        let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
            .await
            .expect("timed out")
            .expect("recv failed");
        let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
        assert_eq!(parsed["event"], "event/with:special.chars-and_underscores");
    }

    // -------------------------------------------------------------------------
    // Sequential events and ordering
    // -------------------------------------------------------------------------

    #[tokio::test]
    async fn events_received_in_order() {
        let bus = EventBus::new(10);
        let mut rx = bus.subscribe();

        for i in 0..5 {
            bus.emit("ordered_event", &i);
        }

        for expected in 0..5 {
            let msg = tokio::time::timeout(Duration::from_millis(50), rx.recv())
                .await
                .expect("timed out")
                .expect("recv failed");
            let parsed: serde_json::Value = serde_json::from_str(&msg).expect("invalid json");
            assert_eq!(parsed["data"], expected);
        }
    }

    // -------------------------------------------------------------------------
    // DEFAULT_BUFFER constant tests
    // -------------------------------------------------------------------------

    #[test]
    fn default_buffer_has_expected_value() {
        let _guard = test_guard!();
        assert_eq!(DEFAULT_BUFFER, 256);
    }

    #[tokio::test]
    async fn new_with_zero_buffer_uses_default() {
        let bus = EventBus::new(0);
        let mut rx = bus.subscribe();

        // Should be able to send DEFAULT_BUFFER messages without lag
        for idx in 0..DEFAULT_BUFFER {
            bus.sender.send(idx.to_string()).unwrap();
        }

        let first = rx.recv().await.expect("recv should not lag");
        assert_eq!(first, "0");
    }
}