fetchlet 0.1.0

Rust-hosted JavaScript functions and Fetch handlers with explicit host services
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
#![cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]

//! Several instances behind one `AppHandle`: the shared queue, growth to
//! `max`, retirement of idle instances, per-instance state, reentrancy and
//! shutdown.

use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, OnceLock};
use std::time::Duration;

use serde_json::{Value, json};
use tokio::sync::{Semaphore, mpsc};

use fetchlet::{
    App, AppHandle, Backend, Body, Error, FetchPolicy, FetchService, HostServices, Instances,
    Module, PoolState, Response, WorkerOptions, transport_fn,
};

const SOURCE: &str = include_str!("fixtures/pool.js");

/// A name no instance has had, which the first call to an instance keeps.
fn unique_name() -> Value {
    static NEXT: AtomicUsize = AtomicUsize::new(0);
    json!(format!("instance-{}", NEXT.fetch_add(1, Ordering::Relaxed)))
}

fn backends() -> Vec<Backend> {
    vec![
        #[cfg(feature = "quickjs")]
        Backend::quickjs(),
        #[cfg(feature = "boa")]
        Backend::boa(),
        #[cfg(feature = "v8")]
        Backend::v8(),
    ]
}

fn count(value: usize) -> NonZeroUsize {
    NonZeroUsize::new(value).unwrap()
}

/// The host side of the fixture's fetches: `gate.test` reports that a call
/// entered it and waits for a permit; `reenter.test` calls the application.
struct Host {
    entered: mpsc::UnboundedSender<()>,
    gate: Arc<Semaphore>,
    application: OnceLock<AppHandle>,
}

struct Pool {
    handle: AppHandle,
    entered: mpsc::UnboundedReceiver<()>,
    gate: Arc<Semaphore>,
}

impl Pool {
    async fn spawn(backend: Backend, instances: Instances) -> fetchlet::Result<Self> {
        let (entered, receive) = mpsc::unbounded_channel();
        let host = Arc::new(Host {
            entered,
            gate: Arc::new(Semaphore::new(0)),
            application: OnceLock::new(),
        });
        let transport_host = host.clone();
        let service = FetchService::new(
            transport_fn(move |request: fetchlet::Request| {
                let host = transport_host.clone();
                async move {
                    if request.uri().host() == Some("reenter.test") {
                        let application = host.application.get().expect("handle is set");
                        application.call_json("instance", &[unique_name()]).await?;
                    } else {
                        host.entered.send(()).unwrap();
                        host.gate.acquire().await.unwrap().forget();
                    }
                    Ok(Response::new(Body::empty()))
                }
            }),
            FetchPolicy::deny_all()
                .allow_origin("https://gate.test")?
                .allow_origin("https://reenter.test")?,
        );
        let mut options = WorkerOptions::new(count(8));
        options.instances = instances;
        let handle = App::builder(backend)
            .module(Module::new(SOURCE))
            .host_services(HostServices::new().outbound(service))
            .spawn(options)
            .await?;
        host.application.set(handle.clone()).ok().unwrap();
        Ok(Self {
            handle,
            entered: receive,
            gate: host.gate.clone(),
        })
    }

    /// Start `wait` and return once it is inside the host fetch.
    async fn start_waiting(&mut self) -> tokio::task::JoinHandle<fetchlet::Result<Value>> {
        let handle = self.handle.clone();
        let task = tokio::spawn(async move { handle.call_json("wait", &[unique_name()]).await });
        within("the call to enter the host fetch", self.entered.recv())
            .await
            .unwrap();
        task
    }

    async fn instance(&self) -> fetchlet::Result<String> {
        Ok(self
            .handle
            .call_json("instance", &[unique_name()])
            .await?
            .as_str()
            .unwrap()
            .to_owned())
    }
}

/// The pool's state once `condition` holds of it.
async fn state_when(handle: &AppHandle, condition: impl Fn(&PoolState) -> bool) -> PoolState {
    within("the pool state", async {
        loop {
            let state = handle.pool_state();
            if condition(&state) {
                return state;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    })
    .await
}

async fn within<T>(what: &str, future: impl Future<Output = T>) -> T {
    match tokio::time::timeout(Duration::from_secs(10), future).await {
        Ok(output) => output,
        Err(_) => panic!("timed out waiting for {what}"),
    }
}

#[tokio::test]
async fn a_busy_instance_does_not_hold_the_queue() -> anyhow::Result<()> {
    for backend in backends() {
        let mut pool = Pool::spawn(backend, Instances::fixed(count(2))).await?;
        let waiting = pool.start_waiting().await;

        // The other instance answers while the first waits.
        let other = within("step 1", pool.instance()).await?;
        assert!(!waiting.is_finished());

        pool.gate.add_permits(1);
        let first = within("step 2", waiting).await??;
        assert_ne!(first.as_str(), Some(other.as_str()));
        pool.handle.shutdown().await?;
    }
    Ok(())
}

#[tokio::test]
async fn one_instance_serves_calls_in_turn() -> anyhow::Result<()> {
    for backend in backends() {
        let mut pool = Pool::spawn(backend, Instances::default()).await?;
        let waiting = pool.start_waiting().await;

        let mut queued = Box::pin(pool.instance());
        assert!(
            tokio::time::timeout(Duration::from_millis(200), &mut queued)
                .await
                .is_err()
        );

        pool.gate.add_permits(1);
        let first = within("step 3", waiting).await??;
        assert_eq!(
            first.as_str(),
            Some(within("step 4", queued).await?.as_str())
        );
        pool.handle.shutdown().await?;
    }
    Ok(())
}

#[tokio::test]
async fn the_pool_grows_on_demand_and_retires_idle_instances() -> anyhow::Result<()> {
    for backend in backends() {
        let instances = Instances {
            min: count(1),
            max: count(2),
            idle_timeout: Some(Duration::from_millis(100)),
            max_held_connections: 0,
        };
        let mut pool = Pool::spawn(backend, instances).await?;
        let first = pool.instance().await?;

        // A queued call starts a second instance while the first waits.
        let waiting = pool.start_waiting().await;
        let grown = within("step 5", pool.instance()).await?;
        assert_ne!(grown, first);
        pool.gate.add_permits(1);
        assert_eq!(
            within("step 6", waiting).await??.as_str(),
            Some(first.as_str())
        );

        // Beyond max, a call waits for a free instance. Either instance may
        // have retired since the calls above, so the busy pair is named by
        // the calls that hold it.
        let first_waiting = pool.start_waiting().await;
        let second_waiting = pool.start_waiting().await;
        let mut queued = Box::pin(pool.instance());
        assert!(
            tokio::time::timeout(Duration::from_millis(200), &mut queued)
                .await
                .is_err()
        );
        pool.gate.add_permits(2);
        let busy = [
            within("step 7", first_waiting).await??,
            within("step 8", second_waiting).await??,
        ];
        assert_ne!(busy[0], busy[1]);
        let served = json!(within("step 9", queued).await?);
        assert!(busy.contains(&served));

        // Once idle past the timeout, the pool is back to one of the two;
        // a second concurrent call needs a new instance.
        state_when(&pool.handle, |state| {
            (state.building, state.busy, state.idle, state.retiring) == (0, 0, 1, 0)
        })
        .await;
        let waiting = pool.start_waiting().await;
        let fresh = json!(within("step 10", pool.instance()).await?);
        pool.gate.add_permits(1);
        let survivor = within("step 11", waiting).await??;
        assert!(busy.contains(&survivor));
        assert!(!busy.contains(&fresh));
        pool.handle.shutdown().await?;
    }
    Ok(())
}

#[tokio::test]
async fn each_instance_keeps_its_own_state() -> anyhow::Result<()> {
    for backend in backends() {
        let mut pool = Pool::spawn(backend, Instances::fixed(count(2))).await?;
        // With one instance waiting, every other call reaches the second.
        let waiting = pool.start_waiting().await;
        for expected in 1..=3 {
            assert_eq!(
                within("step 12", pool.handle.call_json("calls", &[])).await?,
                json!(expected)
            );
        }
        pool.gate.add_permits(1);
        within("step 13", waiting).await??;
        pool.handle.shutdown().await?;
    }
    Ok(())
}

#[tokio::test]
async fn a_host_fetch_cannot_call_its_own_pool() -> anyhow::Result<()> {
    for backend in backends() {
        let pool = Pool::spawn(backend, Instances::fixed(count(2))).await?;
        // Refused even though another instance is idle.
        assert_eq!(
            within("step 14", pool.handle.call_json("reenter", &[])).await?,
            json!("REENTRANT")
        );
        pool.handle.shutdown().await?;
    }
    Ok(())
}

#[tokio::test]
async fn shutdown_finishes_accepted_calls_on_every_instance() -> anyhow::Result<()> {
    for backend in backends() {
        let mut pool = Pool::spawn(backend, Instances::fixed(count(2))).await?;
        let first = pool.start_waiting().await;
        let second = pool.start_waiting().await;

        let handle = pool.handle.clone();
        let shutdown = tokio::spawn(async move { handle.shutdown().await });
        tokio::time::sleep(Duration::from_millis(100)).await;
        assert!(!shutdown.is_finished());
        assert!(matches!(
            pool.handle.call_json("instance", &[unique_name()]).await,
            Err(Error::WorkerClosed)
        ));

        pool.gate.add_permits(2);
        within("step 15", first).await??;
        within("step 16", second).await??;
        within("step 17", shutdown).await??;
    }
    Ok(())
}

#[tokio::test]
async fn min_above_max_is_a_configuration_error() -> anyhow::Result<()> {
    for backend in backends() {
        let instances = Instances {
            min: count(2),
            max: count(1),
            idle_timeout: None,
            max_held_connections: 0,
        };
        assert!(matches!(
            Pool::spawn(backend, instances).await,
            Err(Error::Configuration(_))
        ));
    }
    Ok(())
}

#[tokio::test]
async fn shutdown_without_calls_finishes() -> anyhow::Result<()> {
    for backend in backends() {
        for instances in [Instances::default(), Instances::fixed(count(2))] {
            let pool = Pool::spawn(backend, instances).await?;
            within("shutdown", pool.handle.shutdown()).await?;
        }
    }
    Ok(())
}

#[tokio::test]
async fn the_state_follows_instances_and_the_queue() -> anyhow::Result<()> {
    for backend in backends() {
        let instances = Instances {
            min: count(1),
            max: count(2),
            idle_timeout: Some(Duration::from_millis(100)),
            max_held_connections: 0,
        };
        let mut pool = Pool::spawn(backend, instances).await?;
        // A worker reads as busy from its ready report until it waits.
        let state = state_when(&pool.handle, |state| state.idle == 1).await;
        assert_eq!(
            (state.busy, state.building, state.builds),
            (0, 0, 1),
            "{backend:?}"
        );
        assert_eq!(
            (
                state.min,
                state.max,
                state.queue_capacity,
                state.max_held_connections
            ),
            (1, 2, 8, 0)
        );
        assert!(state.build_time > Duration::ZERO);

        let first = pool.start_waiting().await;
        let state = state_when(&pool.handle, |state| state.busy == 1).await;
        assert_eq!((state.idle, state.queued, state.builds), (0, 0, 1));

        // A second waiting call grows the pool to max; a third waits.
        let second = pool.start_waiting().await;
        let state = state_when(&pool.handle, |state| state.busy == 2).await;
        assert_eq!(state.builds, 2);
        let handle = pool.handle.clone();
        let queued = tokio::spawn(async move { handle.call_json("calls", &[]).await });
        let state = state_when(&pool.handle, |state| state.queued == 1).await;
        assert_eq!((state.busy, state.idle, state.builds), (2, 0, 2));

        pool.gate.add_permits(2);
        within("first", first).await??;
        within("second", second).await??;
        within("queued", queued).await??;

        // Past the idle timeout, the instance above min stops.
        let state = state_when(&pool.handle, |state| state.idle_retirements == 1).await;
        assert_eq!(state.replaced, 0);
        state_when(&pool.handle, |state| {
            (state.idle, state.busy, state.retiring, state.queued) == (1, 0, 0, 0)
        })
        .await;
        pool.handle.shutdown().await?;
    }
    Ok(())
}

#[tokio::test]
async fn an_abandoned_call_counts_its_replacement() -> anyhow::Result<()> {
    for backend in backends() {
        let mut pool = Pool::spawn(backend, Instances::default()).await?;
        let waiting = pool.start_waiting().await;
        waiting.abort();
        let state = state_when(&pool.handle, |state| state.replaced == 1 && state.idle == 1).await;
        assert_eq!(
            (
                state.builds,
                state.failed_builds,
                state.busy,
                state.building
            ),
            (2, 0, 0, 0),
            "{backend:?}"
        );
        pool.handle.shutdown().await?;
    }
    Ok(())
}

#[tokio::test]
async fn the_state_counts_held_and_refused_connections() -> anyhow::Result<()> {
    for backend in backends() {
        let instances = Instances {
            max_held_connections: 1,
            ..Instances::fixed(count(2))
        };
        let pool = Pool::spawn(backend, instances).await?;
        let place = pool.handle.reserve_connection().expect("a free place");
        assert!(pool.handle.reserve_connection().is_none());
        let state = pool.handle.pool_state();
        assert_eq!((state.held_connections, state.refused_connections), (1, 1));
        drop(place);
        let state = pool.handle.pool_state();
        assert_eq!((state.held_connections, state.refused_connections), (0, 1));
        pool.handle.shutdown().await?;
    }
    Ok(())
}