mln-rs-engine 0.0.18

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
// Port-only: the tile store drives each tile's jobs (tile/job.rs): one job
// in flight and a generation, results applied only when the generation
// matches, a geometry tile's dependencies resolved from the store's glyphs
// and images between jobs, and jobs lost with their lane run again. Raster
// and raster-dem tiles' decodes go the same way.

use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
use std::sync::Arc;

use feature::{Feature, PropertyMap};
use geo_types::{Geometry, Point};
use mln_style::bitmap::PremultipliedImage;
use mln_style::conversion::{Convertible, Error as ConversionError};
use mln_style::hash::HashSet;
use mln_style::image::Impl as ImageImpl;
use mln_style::layer::convert_layer;
use mln_style::property::evaluation::PropertyEvaluationParameters;
use mln_style::size::Size;
use mln_style::subdivision_granularity::SubdivisionGranularitySetting;
use mln_style::tile_id::CanonicalTileID;
use parking_lot::Mutex;
use serde_json::json;

use crate::Registry;
use crate::draw::dynamic_texture::DynamicTextureBinId;
use crate::exec::pool::TaskGroup;
use crate::exec::queue::Queue;
use crate::layer::layer_type::ErasedEvaluatedLayer;
use crate::layer::tile_layout::LayoutParams;
use crate::map::MapMode;
use crate::render::events::RendererEvent;
use crate::storage::FileSource;
use crate::storage::stub_file_source::{ResponseType, StubFileSource};
use crate::tests::tile::png;
use crate::tests::{OneTile, style, tile_store};
use crate::tile::{OverscaledTileID, TileData};
use crate::tile_store::contents::Contents;
use crate::tile_store::inbox::{Inbox, InboxSender, TileEvent};
use crate::tile_store::tile::TileKind;
use crate::worker::job::{DataInput, Job, JobId, JobResult, JobTimes, LayoutJob, Reply, Run};
use crate::worker::tile_workers::{InlineWorkers, Lane, PoolWorkers, ReplyBody, TileWorkers};
use crate::worker::worker_codec::{Encoded, RemoteWorkers, ReplySink, TileWorkerHost, WorkerInstances};

/// Workers whose jobs run when the test says so, or get the replies the
/// test makes up.
#[derive(Clone, Default)]
pub(super) struct Manual(Rc<RefCell<ManualState>>);

#[derive(Default)]
struct ManualState {
    runs: VecDeque<Run>,
    inbox: Option<InboxSender>,
    caches_data: bool,
    submitted: usize,
}

impl Manual {
    /// The oldest job submitted and not yet answered.
    pub(super) fn take(&self) -> Run {
        self.0.borrow_mut().runs.pop_front().expect("a job in flight")
    }

    pub(super) fn reply(&self, Reply { id, result }: Reply) {
        let inbox = self.0.borrow().inbox.clone().expect("a job submitted");
        inbox.send(TileEvent::Reply { lane: Lane(0), id, body: ReplyBody::Result(result), times: JobTimes::default() });
    }

    /// Runs the oldest job in flight; returns its id.
    pub(super) fn run(&self) -> JobId {
        let reply = InlineWorkers::run(self.take());
        let id = reply.id;
        self.reply(reply);
        id
    }

    pub(super) fn submitted(&self) -> usize {
        self.0.borrow().submitted
    }
}

impl TileWorkers for Manual {
    fn lanes(&self) -> usize {
        1
    }

    fn capacity(&self, _lane: Lane) -> usize {
        2
    }

    fn caches_data(&self) -> bool {
        self.0.borrow().caches_data
    }

    fn submit(&mut self, _lane: Lane, run: Run, inbox: &InboxSender) {
        let mut state = self.0.borrow_mut();
        state.inbox = Some(inbox.clone());
        state.submitted += 1;
        state.runs.push_back(run);
    }
}

pub(super) fn image(id: &str) -> Arc<ImageImpl> {
    Arc::new(
        ImageImpl::new(
            id.to_owned(),
            PremultipliedImage::new(Size::new(4, 4)),
            1.0,
            false,
            vec![],
            vec![],
            None,
            None,
            None,
        )
        .expect("image"),
    )
}

/// A layer from its style JSON on source `s`, evaluated at zoom 0 as the
/// `RenderOrchestrator` evaluates it.
pub(super) fn layer(mut json: serde_json::Value) -> Arc<dyn ErasedEvaluatedLayer> {
    json["source"] = json!("s");
    let mut error = ConversionError::default();
    let layer = convert_layer(Registry::with_builtins().style(), &Convertible::from(&json), &mut error)
        .unwrap_or_else(|| panic!("{}", error.message));
    let mut render_layer = crate::tests::render_layer(layer.style());
    render_layer.evaluate(&PropertyEvaluationParameters::new(0.0));
    Arc::clone(&render_layer.base().evaluated_properties)
}

/// Icons of `icon-image`, at size `size`.
pub(super) fn icons(icon_image: &serde_json::Value, size: f32) -> Arc<dyn ErasedEvaluatedLayer> {
    layer(json!({"id": "icons", "type": "symbol", "layout": {"icon-image": icon_image, "icon-size": size}}))
}

pub(super) fn points() -> TileData {
    let point = Feature {
        geometry: Some(Geometry::Point(Point::new(2048i16, 2048))),
        properties: Arc::new(PropertyMap::new()),
        id: feature::Identifier::Null,
    };
    TileData::Features(Arc::new(vec![point]))
}

struct Test {
    thread_pool: TaskGroup,
    tile: OneTile,
    /// The images the store asked the app for.
    asked: Vec<String>,
    /// What the glyph and image stores reported, in order.
    resource_events: Vec<RendererEvent>,
}

impl Test {
    /// A tile whose jobs run on `workers`, with a loaded sprite of `images`.
    fn new(workers: Box<dyn TileWorkers>, images: &[&str]) -> Self {
        let thread_pool = TaskGroup::new(&crate::tests::pool());
        let mut store = tile_store(&thread_pool, workers);
        for id in images {
            store.images_mut().add_image(image(id));
        }
        store.images_mut().set_loaded(true);
        let id = OverscaledTileID::new(0, 0, CanonicalTileID::new(0, 0, 0));
        let tile = OneTile::new(store, TileKind::Geometry, id, MapMode::Static);
        Test { thread_pool, tile, asked: Vec::new(), resource_events: Vec::new() }
    }

    /// The renderer's update.
    fn update(&mut self) -> bool {
        let updated = self.tile.update();
        for event in self.tile.store.take_resource_events() {
            if let RendererEvent::StyleImageMissing(id) = &event {
                self.asked.push(id.clone());
            }
            self.resource_events.push(event);
        }
        updated
    }

    /// A tile with `layers`, whose data arrived.
    fn load(&mut self, layers: &[Arc<dyn ErasedEvaluatedLayer>]) {
        self.tile.set_style(&style(layers));
        self.tile.set_data(Some(points()));
    }

    fn is_complete(&self) -> bool {
        self.tile.tile().is_complete()
    }

    fn shows_layout(&self) -> bool {
        self.tile.shows_layout()
    }

    /// Adds image `id` to the store, and tells the tiles.
    fn add_image(&mut self, id: &str) {
        self.tile.store.images_mut().add_image(image(id));
        self.tile.store.images_changed(&HashSet::from_iter([id.to_owned()]));
    }

    /// The id of the bin the atlas packs `id` into now.
    fn bin_of(&mut self, id: &str) -> DynamicTextureBinId {
        let image = self.tile.store.images().image(id).cloned().expect("image");
        let icons = [(id.to_owned(), image)].into_iter().collect();
        let page = self.tile.store.atlas().pack_images(&icons, &crate::image::image_store::ImageMap::default());
        page.lease.texture_handles()[0].id()
    }
}

impl Drop for Test {
    fn drop(&mut self) {
        self.thread_pool.wait();
    }
}

/// A cold tile with icons takes two jobs: the first finds what it needs,
/// the second lays out with it. A relayout carries what the layout used,
/// so a style edit that changes no icons takes one.
#[test]
fn a_cold_labelled_tile_takes_two_jobs_and_a_relayout_one() {
    let manual = Manual::default();
    let mut test = Test::new(Box::new(manual.clone()), &["marker"]);
    test.tile.set_style(&style(&[icons(&json!("marker"), 1.0)]));
    test.tile.set_data(Some(points()));
    test.update();
    let first = manual.run();
    test.update();
    assert!(!test.shows_layout());
    let second = manual.run();
    assert_eq!(first, second, "the resume keeps the generation");
    test.update();
    assert!(test.shows_layout() && test.is_complete());
    assert_eq!(manual.submitted(), 2);

    test.tile.set_style(&style(&[icons(&json!("marker"), 2.0)]));
    test.update();
    {
        let state = manual.0.borrow();
        let Job::Layout(job) = &state.runs[0].job else {
            panic!("not a layout job");
        };
        assert!(job.resolved.icons.contains_key("marker"), "the relayout carries the icon's position");
    }
    manual.run();
    test.update();
    assert!(test.is_complete());
    assert_eq!(manual.submitted(), 3);
}

/// A reply to a job whose tile changed since is stale: the tile keeps
/// showing what it showed, and lays its newest generation out.
#[test]
fn a_stale_reply_after_a_relayout_is_dropped() {
    let manual = Manual::default();
    let mut test = Test::new(Box::new(manual.clone()), &[]);
    let line = layer(json!({"id": "line", "type": "circle"}));
    test.tile.set_style(&style(std::slice::from_ref(&line)));
    test.tile.set_data(Some(points()));
    test.update();
    let stale = manual.take();

    test.tile.set_style(&style(&[line]));
    let reply = InlineWorkers::run(stale);
    assert!(matches!(reply.result, JobResult::Laid(_)));
    manual.reply(reply);
    test.update();
    assert!(!test.shows_layout(), "a stale layout isn't installed");
    assert_eq!(manual.submitted(), 2, "the newest generation went once the stale job came back");
    manual.run();
    test.update();
    assert!(test.shows_layout() && test.is_complete());
}

/// A job whose data the worker evicted answers `NeedsData`: the tile sends
/// the same job again, with its data.
#[test]
fn a_tile_sends_its_data_again_after_an_eviction() {
    let manual = Manual::default();
    manual.0.borrow_mut().caches_data = true;
    let mut test = Test::new(Box::new(manual.clone()), &["marker"]);
    test.tile.set_style(&style(&[icons(&json!("marker"), 1.0)]));
    test.tile.set_data(Some(points()));
    test.update();
    let first = manual.take();
    assert!(matches!(&first.job, Job::Layout(job) if matches!(job.data, DataInput::Full { .. })));
    manual.reply(InlineWorkers::run(first));
    test.update();
    let resume = manual.take();
    let Job::Layout(job) = &resume.job else {
        panic!("not a layout job");
    };
    assert!(matches!(job.data, DataInput::Cached(_)), "the lane that had the data gets its key");
    manual.reply(Reply { id: resume.id, result: JobResult::NeedsData });
    test.update();
    let again = manual.take();
    assert_eq!(again.id, resume.id);
    let Job::Layout(job) = &again.job else {
        panic!("not a layout job");
    };
    assert!(matches!(job.data, DataInput::Full { .. }));
    assert!(job.resolved.icons.contains_key("marker"), "with the same answer");
    manual.reply(InlineWorkers::run(again));
    test.update();
    assert!(test.shows_layout());
}

/// A first try whose worker lacked the data runs again with the data, and
/// with the images as they are then: an image added since is there.
#[test]
fn a_first_try_sent_again_gets_the_current_images() {
    let manual = Manual::default();
    manual.0.borrow_mut().caches_data = true;
    let mut test = Test::new(Box::new(manual.clone()), &[]);
    test.load(&[layer(json!({"id": "dots", "type": "circle"}))]);
    test.update();
    manual.run();
    test.update();
    assert!(test.shows_layout());

    // The relayout's first try names the data its lane got.
    test.tile.set_style(&style(&[layer(json!({"id": "dots", "type": "circle"}))]));
    test.update();
    let first = manual.take();
    assert!(matches!(&first.job, Job::Layout(job) if matches!(job.data, DataInput::Cached(_))));
    test.tile.store.images_mut().add_image(image("late"));
    test.tile.store.images_changed(&HashSet::from_iter(["late".to_owned()]));
    manual.reply(Reply { id: first.id, result: JobResult::NeedsData });
    test.update();
    let again = manual.take();
    let Job::Layout(job) = &again.job else {
        panic!("not a layout job");
    };
    assert!(matches!(job.data, DataInput::Full { .. }), "with its data");
    assert!(job.available_images.contains("late"), "with the images as they are");
    manual.reply(InlineWorkers::run(again));
    test.update();
    assert!(test.is_complete());
}

/// A relayout for a changed image starts without the answer the tile shows,
/// and so does every first try until a layout shows: one superseded by a
/// style edit before it replied too.
#[test]
fn a_relayout_for_an_image_never_starts_with_the_outdated_answer() {
    let manual = Manual::default();
    let mut test = Test::new(Box::new(manual.clone()), &["a"]);
    test.load(&[icons(&json!("a"), 1.0)]);
    for _ in 0..2 {
        test.update();
        manual.run();
    }
    test.update();
    assert!(test.shows_layout());

    let resolved = |run: &Run| match &run.job {
        Job::Layout(job) => Arc::clone(&job.resolved),
        Job::Raster { .. } | Job::Dem { .. } => panic!("not a layout job"),
    };
    test.tile.store.images_mut().update_image(image("a"));
    test.tile.store.images_changed(&HashSet::from_iter(["a".to_owned()]));
    test.update();
    let first = manual.take();
    assert!(resolved(&first).icons.is_empty(), "the relayout starts without the icon's position");

    test.tile.set_style(&style(&[icons(&json!("a"), 2.0)]));
    manual.reply(InlineWorkers::run(first));
    test.update();
    let again = manual.take();
    assert!(resolved(&again).icons.is_empty(), "so does the first try after it");
}

/// An image added while a tile's first job runs may change what that job
/// discovers (`coalesce(image(a), image(b))`): its reply is stale, and the
/// tile lays out again with the images as they are.
#[test]
fn an_image_added_while_a_first_job_runs_lays_out_again() {
    let manual = Manual::default();
    let mut test = Test::new(Box::new(manual.clone()), &[]);
    test.tile.set_style(&style(&[icons(&json!(["coalesce", ["image", "late"], ["image", "other"]]), 1.0)]));
    test.tile.set_data(Some(points()));
    test.update();
    let first = manual.take();

    test.add_image("late");
    manual.reply(InlineWorkers::run(first));
    test.update();
    let again = manual.take();
    let Job::Layout(job) = &again.job else {
        panic!("not a layout job");
    };
    assert!(job.available_images.contains("late"));
    manual.reply(InlineWorkers::run(again));
    test.update();
    let resume = manual.take();
    let Job::Layout(job) = &resume.job else {
        panic!("not a layout job");
    };
    assert!(job.resolved.icons.contains_key("late"));
}

/// An answer's bins stay leased while its job runs and while its layout
/// shows: a bin a worker was told about can't go to another image before.
#[test]
fn an_answer_holds_its_bins_while_its_layout_runs_and_shows() {
    let manual = Manual::default();
    let mut test = Test::new(Box::new(manual.clone()), &["a"]);
    test.tile.set_style(&style(&[icons(&json!("a"), 1.0)]));
    test.tile.set_data(Some(points()));
    test.update();
    manual.run();
    test.update();
    let bin = test.bin_of("a");
    test.tile.store.atlas().free_released();
    assert_eq!(test.bin_of("a"), bin, "the running job holds the bin");

    manual.run();
    test.update();
    assert!(test.shows_layout());
    test.tile.store.atlas().free_released();
    assert_eq!(test.bin_of("a"), bin, "the layout holds the bin");

    test.tile.drop_tile();
    test.tile.store.atlas().free_released();
    assert_eq!(test.tile.store.atlas().page_count(), 0, "nothing holds the bin");
}

/// A tile that waits for both glyphs and a missing image reports the glyph
/// range it requests, then the image it asks the app for: the order the
/// stores were asked in.
#[test]
fn glyph_and_image_events_keep_their_order() {
    let manual = Manual::default();
    let mut test = Test::new(Box::new(manual.clone()), &[]);
    let run_loop: Queue<()> = Queue::new();
    let file_source: Arc<dyn FileSource> = StubFileSource::on_queue(&run_loop, ResponseType::Asynchronous);
    test.tile.store.set_file_source(Some(file_source));
    test.tile.store.glyphs().set_url("test://{fontstack}/{range}.pbf");
    test.load(&[layer(json!({
        "id": "labels",
        "type": "symbol",
        "layout": {"text-field": "a", "icon-image": "missing"}
    }))]);
    test.update();
    manual.run();
    test.update();

    let kinds: Vec<&str> = test
        .resource_events
        .iter()
        .filter_map(|event| match event {
            RendererEvent::GlyphsRequested { .. } => Some("glyphs"),
            RendererEvent::StyleImageMissing(_) => Some("image"),
            _ => None,
        })
        .collect();
    assert_eq!(kinds, ["glyphs", "image"]);
}

/// The stores resolve only when they have something new: a tile that waits
/// for a missing image asks the app for it once, and resumes knowing it
/// missing in the next update.
#[test]
fn a_missing_image_resolves_as_missing_in_the_next_update() {
    let manual = Manual::default();
    let mut test = Test::new(Box::new(manual.clone()), &[]);
    test.load(&[icons(&json!("missing"), 1.0)]);
    test.update();
    manual.run();
    test.update();
    assert_eq!(test.asked, ["missing"]);
    assert_eq!(manual.submitted(), 1, "the app has an update to add it");

    test.update();
    let resume = manual.take();
    let Job::Layout(job) = &resume.job else {
        panic!("not a layout job");
    };
    assert!(job.resolved.missing_images.contains("missing"));
    assert_eq!(test.asked.len(), 1);
    manual.reply(InlineWorkers::run(resume));
    test.update();
    assert!(test.shows_layout());
}

/// Removing an image the shown layout used lays the tile out again.
#[test]
fn removing_a_used_image_lays_out_again() {
    let manual = Manual::default();
    let mut test = Test::new(Box::new(manual.clone()), &["a"]);
    test.load(&[icons(&json!("a"), 1.0)]);
    for _ in 0..2 {
        test.update();
        manual.run();
    }
    test.update();
    assert!(test.is_complete());

    test.tile.store.images_mut().remove_image("a");
    test.tile.store.images_changed(&HashSet::from_iter(["b".to_owned()]));
    assert!(test.is_complete(), "an image it didn't name changes nothing");
    test.tile.store.images_changed(&HashSet::from_iter(["a".to_owned()]));
    assert!(!test.is_complete());
    test.update();
    assert_eq!(manual.submitted(), 3);
}

/// A relayout that no longer uses a missing image drops it from the answer
/// it carries: the image, added while unused, is there when a later layout
/// uses it again.
#[test]
fn a_missing_image_a_relayout_drops_resolves_when_used_again() {
    let manual = Manual::default();
    let mut test = Test::new(Box::new(manual.clone()), &[]);
    test.load(&[icons(&json!("a"), 1.0)]);
    for _ in 0..2 {
        test.update();
        test.update();
        manual.run();
    }
    test.update();
    assert!(test.shows_layout());

    test.load(&[layer(json!({"id": "dots", "type": "circle"}))]);
    test.update();
    manual.run();
    test.update();
    test.add_image("a");

    test.load(&[icons(&json!("a"), 1.0)]);
    test.update();
    let again = manual.take();
    let Job::Layout(job) = &again.job else {
        panic!("not a layout job");
    };
    assert!(!job.resolved.missing_images.contains("a"), "the answer it carries forgets the missing image");
}

/// Evaluated values of no layer type: a bug that panics when the layout
/// asks for their type.
struct Mistyped(Arc<dyn mln_style::layer::ErasedLayerStyle>);

impl ErasedEvaluatedLayer for Mistyped {
    fn entry(&self) -> &'static dyn crate::layer::layer_type::ErasedLayerType {
        panic!("evaluated values of no layer type")
    }
    fn style(&self) -> &dyn mln_style::layer::ErasedLayerStyle {
        &*self.0
    }
    fn shared_style(&self) -> Arc<dyn mln_style::layer::ErasedLayerStyle> {
        Arc::clone(&self.0)
    }
    fn constants_mask(&self) -> u64 {
        0
    }
    fn evaluated_dependencies(&self) -> mln_style::expression::Dependency {
        mln_style::expression::Dependency::NONE
    }
    fn collect_evaluated_global_state_refs(&self, _refs: &mut mln_vec_map::VecSet<String>) {}
    fn write(&self, w: &mut mln_wire::Writer) {
        w.fail("no wire form".to_owned());
    }
}

/// A job that panics on the thread pool still replies, `Failed`, so it
/// doesn't hold a place in flight for good.
#[test]
fn a_panicking_native_job_fails() {
    let pool = TaskGroup::new(&crate::tests::pool());
    let mut workers = PoolWorkers::new(pool, 1);
    let inbox = Inbox::new(Arc::new(|| {}));
    let circles = layer(json!({"id": "dots", "type": "circle"}));
    let mistyped: Arc<dyn ErasedEvaluatedLayer> = Arc::new(Mistyped(circles.shared_style()));
    let id = JobId {
        tile: crate::worker::job::TileKey {
            source: crate::worker::job::SourceKey(1),
            id: OverscaledTileID::from_zxy(0, 0, 0),
        },
        generation: 1,
    };
    let key = crate::worker::job::DataKey { source: id.tile.source, tile: id.tile.id.canonical, load: 1 };
    let job = LayoutJob {
        style: style(&[mistyped]),
        available_images: Arc::default(),
        data: DataInput::Full { key, data: Some(points()) },
        params: LayoutParams {
            mode: MapMode::Static,
            pixel_ratio: 1.0,
            subdivision_granularity: SubdivisionGranularitySetting::none(),
            first_load: true,
        },
        resolved: Arc::default(),
    };
    workers.submit(Lane(0), Run { id, job: Job::Layout(job), superseded: Arc::default() }, inbox.sender());
    let deadline = mln_style::time::Instant::now() + std::time::Duration::from_secs(10);
    let event = loop {
        if let Some(event) = inbox.next() {
            break event;
        }
        assert!(mln_style::time::Instant::now() < deadline, "no reply");
        std::thread::yield_now();
    };
    let TileEvent::Reply { id: replied, body: ReplyBody::Result(JobResult::Failed(error)), .. } = event else {
        panic!("expected a failure");
    };
    assert_eq!(replied, id);
    assert_eq!(error.to_string(), "the tile's job panicked");
}

/// Worker instances on the test's thread, whose frames wait until the test
/// passes them on, and which the test can kill.
#[derive(Clone)]
struct Instances(Rc<RefCell<InstancesState>>);

struct InstancesState {
    sink: Option<ReplySink>,
    hosts: Vec<Option<TileWorkerHost>>,
    queued: Vec<VecDeque<Vec<u8>>>,
    replies: Arc<Mutex<Vec<(usize, Encoded)>>>,
    started: usize,
}

impl Instances {
    fn new(count: usize) -> Self {
        let replies = Arc::new(Mutex::new(Vec::new()));
        let hosts = (0..count).map(|instance| Some(Self::host(instance, &replies))).collect();
        Instances(Rc::new(RefCell::new(InstancesState {
            sink: None,
            hosts,
            queued: (0..count).map(|_| VecDeque::new()).collect(),
            replies,
            started: count,
        })))
    }

    fn host(instance: usize, replies: &Arc<Mutex<Vec<(usize, Encoded)>>>) -> TileWorkerHost {
        let replies = Arc::clone(replies);
        TileWorkerHost::new(Registry::with_builtins(), move |reply| replies.lock().push((instance, reply)))
    }

    /// `RemoteWorkers` over these instances.
    fn workers(&self) -> Box<dyn TileWorkers> {
        let instances = self.clone();
        Box::new(RemoteWorkers::new(Registry::with_builtins(), move |sink| {
            instances.0.borrow_mut().sink = Some(sink);
            Box::new(instances)
        }))
    }

    /// Runs the frames that wait for `instance`, and sends their replies.
    fn step(&self, instance: usize) {
        let state = self.0.borrow();
        let frames: Vec<Vec<u8>> = state.queued[instance].iter().cloned().collect();
        drop(state);
        self.0.borrow_mut().queued[instance].clear();
        for frame in frames {
            self.0.borrow().hosts[instance].as_ref().expect("a live instance").handle(&frame);
        }
        let state = self.0.borrow();
        let sink = state.sink.as_ref().expect("a sink");
        for (instance, reply) in state.replies.lock().drain(..) {
            sink.send(instance, reply);
        }
    }

    /// Ends `instance` with what waits for it, as a trap ends a Web
    /// Worker, which reports it.
    fn kill(&self, instance: usize) {
        let mut state = self.0.borrow_mut();
        state.hosts[instance] = None;
        state.queued[instance].clear();
        state.sink.as_ref().expect("a sink").lost(instance);
    }

    fn queued(&self, instance: usize) -> usize {
        self.0.borrow().queued[instance].len()
    }
}

impl WorkerInstances for Instances {
    fn count(&self) -> usize {
        self.0.borrow().hosts.len()
    }

    fn post(&mut self, instance: usize, request: Vec<u8>) {
        self.0.borrow_mut().queued[instance].push_back(request);
    }

    fn replace(&mut self, instance: usize) -> bool {
        let mut state = self.0.borrow_mut();
        let host = Self::host(instance, &state.replies);
        state.hosts[instance] = Some(host);
        state.queued[instance].clear();
        state.started += 1;
        true
    }
}

/// A Web Worker that dies while it discovers a tile's dependencies, and
/// another while it lays the tile out with them: each time the tile's job
/// runs again, on a new worker with new tables, and the tile lays out.
#[test]
fn a_worker_lost_during_discovery_and_during_a_resume() {
    let instances = Instances::new(1);
    let mut test = Test::new(instances.workers(), &["marker"]);
    test.load(&[icons(&json!("marker"), 1.0)]);
    test.update();
    assert_eq!(instances.queued(0), 1);

    // During discovery.
    instances.kill(0);
    test.update();
    assert_eq!(instances.0.borrow().started, 2);
    assert_eq!(instances.queued(0), 1, "the first try runs again, on the new worker");
    instances.step(0);
    test.update();
    assert_eq!(instances.queued(0), 1, "the resume");

    // During the resume.
    instances.kill(0);
    test.update();
    assert_eq!(instances.queued(0), 1, "the first try runs again");
    assert!(!test.shows_layout());
    for _ in 0..2 {
        instances.step(0);
        test.update();
    }
    assert!(test.shows_layout() && test.is_complete());
}

/// A reply whose header the map side can't read loses its worker too.
#[test]
fn an_unreadable_reply_loses_its_worker() {
    let instances = Instances::new(2);
    let mut test = Test::new(instances.workers(), &[]);
    test.load(&[layer(json!({"id": "dots", "type": "circle"}))]);
    test.update();
    let lane = (0..2).find(|lane| instances.queued(*lane) == 1).expect("a lane with the job");
    instances.0.borrow().sink.as_ref().unwrap().send(lane, Encoded::from(vec![0, 1, 2]));
    test.update();
    assert_eq!(instances.0.borrow().started, 3);
    let queued: usize = (0..2).map(|lane| instances.queued(lane)).sum();
    assert_eq!(queued, 1, "the job runs again");
    for lane in 0..2 {
        instances.step(lane);
    }
    test.update();
    assert!(test.shows_layout());
}

/// What the instances of replaced workers send, a reply or their loss,
/// reaches only those workers, not the ones in their place.
#[test]
fn replaced_workers_reach_no_other_workers() {
    let (old, new) = (Instances::new(1), Instances::new(1));
    let mut test = Test::new(old.workers(), &[]);
    test.load(&[layer(json!({"id": "dots", "type": "circle"}))]);
    test.update();
    assert_eq!(old.queued(0), 1);

    test.tile.store.set_workers(new.workers());
    test.update();
    assert_eq!(new.queued(0), 1, "the job runs again on the new workers");
    old.step(0);
    old.kill(0);
    test.update();
    assert_eq!(new.0.borrow().started, 1, "the old instance's loss isn't the new one's");
    assert!(!test.shows_layout(), "nor is its reply");

    new.step(0);
    test.update();
    assert!(test.shows_layout());
}

/// An image added while a tile's first try runs, which the try names but
/// didn't branch on, resolves as it is when the try replies: the tile
/// resumes without discovering again.
#[test]
fn an_image_a_first_try_only_names_resolves_without_a_relayout() {
    let manual = Manual::default();
    let mut test = Test::new(Box::new(manual.clone()), &[]);
    test.load(&[icons(&json!("late"), 1.0)]);
    test.update();
    let first = manual.take();

    test.add_image("late");
    manual.reply(InlineWorkers::run(first));
    test.update();
    assert_eq!(manual.submitted(), 2);
    let resume = manual.take();
    let Job::Layout(job) = &resume.job else {
        panic!("not a layout job");
    };
    assert!(job.resolved.icons.contains_key("late"));
}

/// A tile's job starts in the update that has its data, and a newer
/// generation supersedes the job in flight, which a worker that hasn't
/// started it may skip.
#[test]
fn a_newer_generation_supersedes_the_job_in_flight() {
    let manual = Manual::default();
    let mut test = Test::new(Box::new(manual.clone()), &[]);
    test.tile.set_style(&style(&[layer(json!({"id": "dots", "type": "circle"}))]));
    test.update();
    assert_eq!(manual.submitted(), 0, "no data yet");
    test.tile.set_data(Some(points()));
    test.update();
    assert_eq!(manual.submitted(), 1);

    let first = manual.take();
    test.tile.set_data(Some(points()));
    assert!(first.superseded.load(std::sync::atomic::Ordering::Relaxed));
}

/// A raster tile of `kind`, `x`/0 of zoom 1, and a second one at `x + 1`,
/// whose jobs run on `workers`.
fn raster_tiles(pool: &TaskGroup, workers: Box<dyn TileWorkers>, kind: TileKind) -> OneTile {
    let store = tile_store(pool, workers);
    let mut tiles = OneTile::new(store, kind, OverscaledTileID::from_zxy(1, 0, 0), MapMode::Static);
    tiles.add(OverscaledTileID::from_zxy(1, 1, 0));
    tiles
}

/// The size of the image a raster tile shows.
fn raster_size(tiles: &OneTile, x: u32) -> Option<Size> {
    let Some(Contents::Raster(raster)) = tiles.tile_at(&OverscaledTileID::from_zxy(1, x, 0)).contents() else {
        return None;
    };
    Some(raster.image.size)
}

/// Decodes come back in any order: each tile takes only the reply of its
/// current generation, and one whose data changed while it decoded decodes
/// its newest data next and never shows the stale image.
#[test]
fn raster_decodes_apply_in_any_order() {
    let pool = TaskGroup::new(&crate::tests::pool());
    let manual = Manual::default();
    let mut tiles = raster_tiles(&pool, Box::new(manual.clone()), TileKind::Raster);
    let ids = [OverscaledTileID::from_zxy(1, 0, 0), OverscaledTileID::from_zxy(1, 1, 0)];
    for (id, size) in ids.iter().zip([16, 32]) {
        tiles.set_image(id, Some(png(size, size)));
    }
    tiles.update();
    let (first, second) = (manual.take(), manual.take());
    manual.reply(InlineWorkers::run(second));
    manual.reply(InlineWorkers::run(first));
    tiles.update();
    assert_eq!(raster_size(&tiles, 0), Some(Size::new(16, 16)));
    assert_eq!(raster_size(&tiles, 1), Some(Size::new(32, 32)));

    // New data while the decode of the last runs: the last's reply is stale.
    tiles.set_image(&ids[0], Some(png(8, 8)));
    tiles.update();
    let stale = manual.take();
    tiles.set_image(&ids[0], Some(png(4, 4)));
    assert!(stale.superseded.load(std::sync::atomic::Ordering::Relaxed));
    tiles.update();
    assert_eq!(manual.submitted(), 3, "one decode in flight per tile");
    manual.reply(InlineWorkers::run(stale));
    tiles.update();
    assert_eq!(raster_size(&tiles, 0), Some(Size::new(16, 16)), "the stale image isn't shown");
    assert!(!tiles.tile().is_complete());
    manual.run();
    tiles.update();
    assert_eq!(raster_size(&tiles, 0), Some(Size::new(4, 4)));
    assert!(tiles.tile().is_complete());
    drop(tiles);
    pool.wait();
}

/// A Web Worker that dies while it decodes a raster-dem tile: the tile kept
/// its data, which it sends again to the worker in its place.
#[test]
fn a_worker_lost_during_a_decode() {
    let pool = TaskGroup::new(&crate::tests::pool());
    let instances = Instances::new(1);
    let kind = TileKind::Dem(mln_style::tileset::RasterEncoding::Mapbox);
    let mut tiles = raster_tiles(&pool, instances.workers(), kind);
    let id = tiles.id;
    tiles.set_image(&id, Some(png(16, 16)));
    tiles.update();
    assert_eq!(instances.queued(0), 1);

    instances.kill(0);
    tiles.update();
    assert_eq!(instances.0.borrow().started, 2);
    assert_eq!(instances.queued(0), 1, "the decode runs again, on the new worker");
    assert!(!tiles.tile().is_complete());
    instances.step(0);
    tiles.update();
    assert!(tiles.tile().is_complete() && tiles.tile().is_renderable());
    let Some(Contents::Dem(dem)) = tiles.tile().contents() else {
        panic!("a DEM");
    };
    assert_eq!(dem.dem().dim, 16);
    drop(tiles);
    pool.wait();
}

/// A world copy made of a tile that hasn't uploaded copies the data of its
/// buckets. The tile's upload moves it into the context's stores once, and
/// the copy, laid out from the same data, takes what the upload moved and
/// frees its own.
#[test]
fn a_tiles_upload_frees_the_data_its_world_copy_copied() {
    use crate::buckets::circle_bucket::CircleBucket;
    use crate::layer::bucket_geometry::bucket_vertices;
    use crate::tile_store::source_tiles::SourceTiles;

    let thread_pool = TaskGroup::new(&crate::tests::pool());
    let id = OverscaledTileID::new(0, 0, CanonicalTileID::new(0, 0, 0));
    let copy_id = OverscaledTileID::new(0, 1, CanonicalTileID::new(0, 0, 0));
    let mut tiles =
        OneTile::new(tile_store(&thread_pool, Box::new(InlineWorkers)), TileKind::Geometry, id, MapMode::Continuous);
    tiles.set_style(&style(&[layer(json!({"id": "circles", "type": "circle"}))]));
    tiles.set_data(Some(points()));
    tiles.settle(&[id]);
    tiles.add(copy_id);
    let (source, ctx) = tiles.store.source_mut(tiles.key).expect("the source");
    let copy = source.world_copy(&copy_id).expect("a copy of the tile");
    source.update(&copy_id, ctx, |tile| tile.start_as_copy(copy));
    // Whether the circles' vertices of tile `id` moved out of its bucket and
    // its data is gone, and the stored vertices they moved into.
    let circles = |source: &SourceTiles, id: &OverscaledTileID| {
        let contents = source.tile(id)?.contents()?.geometry()?;
        let mut buckets = contents.result.layer_render_data.buckets();
        let vertices = &buckets.find_map(|bucket| bucket.downcast_ref::<CircleBucket>())?.vertices;
        let stored = vertices.is_moved().then(|| bucket_vertices(&contents.bucket_geometry, vertices)).flatten();
        Some((vertices.is_moved(), vertices.data().is_empty(), stored))
    };
    assert!(matches!(circles(source, &copy_id), Some((false, false, None))), "the copy holds a copy of the data");

    crate::tests::with_context(|_, context| {
        let needs = crate::layers::upload_needs(context);
        source.upload_at(&id, None, &ctx.atlas, context, needs);
        let (tile, copy) = (circles(source, &id), circles(source, &copy_id));
        assert!(matches!(&tile, Some((true, true, Some(_)))), "the tile's data moved");
        assert!(matches!(&copy, Some((true, true, Some(_)))), "the copy freed its data");
        assert!(tile.and_then(|tile| tile.2) == copy.and_then(|copy| copy.2), "both draw the same vertices");
    });
}