-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathmod.rs
More file actions
1097 lines (1008 loc) · 36.8 KB
/
mod.rs
File metadata and controls
1097 lines (1008 loc) · 36.8 KB
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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pub(crate) use temporal_sdk_core_test_utils::canned_histories;
use crate::{
TaskToken, Worker, WorkerConfig, WorkerConfigBuilder,
pollers::{BoxedPoller, MockManualPoller, MockPoller},
protosext::ValidPollWFTQResponse,
replay::TestHistoryBuilder,
sticky_q_name_for_worker,
worker::{
TaskPollers,
client::{
MockWorkerClient, WorkerClient, WorkflowTaskCompletion, mocks::mock_workflow_client,
},
},
};
use async_trait::async_trait;
use bimap::BiMap;
use futures_util::{FutureExt, Stream, StreamExt, future::BoxFuture, stream, stream::BoxStream};
use mockall::TimesRange;
use parking_lot::RwLock;
use std::{
collections::{BTreeMap, HashMap, HashSet, VecDeque},
fmt::Debug,
ops::{Deref, DerefMut},
pin::Pin,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
task::{Context, Poll},
time::Duration,
};
use temporal_sdk::interceptors::FailOnNondeterminismInterceptor;
use temporal_sdk_core_api::{
Worker as WorkerTrait,
errors::PollError,
worker::{PollerBehavior, WorkerVersioningStrategy},
};
use temporal_sdk_core_protos::{
coresdk::{
workflow_activation::{WorkflowActivation, workflow_activation_job},
workflow_commands::workflow_command,
workflow_completion::{self, WorkflowActivationCompletion, workflow_activation_completion},
},
temporal::api::{
common::v1::WorkflowExecution,
enums::v1::WorkflowTaskFailedCause,
failure::v1::Failure,
protocol,
protocol::v1::message,
update,
workflowservice::v1::{
PollActivityTaskQueueResponse, PollNexusTaskQueueResponse,
PollWorkflowTaskQueueResponse, RespondWorkflowTaskCompletedResponse,
},
},
utilities::pack_any,
};
use temporal_sdk_core_test_utils::{NAMESPACE, TestWorker};
use tokio::sync::{Notify, mpsc::unbounded_channel};
use tokio_stream::wrappers::UnboundedReceiverStream;
pub(crate) const TEST_Q: &str = "q";
pub(crate) fn test_worker_cfg() -> WorkerConfigBuilder {
let mut wcb = WorkerConfigBuilder::default();
wcb.namespace(NAMESPACE)
.task_queue(TEST_Q)
.versioning_strategy(WorkerVersioningStrategy::None {
build_id: "test_bin_id".to_string(),
})
.ignore_evicts_on_shutdown(true)
// Serial polling since it makes mocking much easier.
.workflow_task_poller_behavior(PollerBehavior::SimpleMaximum(1_usize));
wcb
}
/// When constructing responses for mocks, indicates how a given response should be built
#[derive(derive_more::From)]
#[allow(clippy::large_enum_variant)] // Test only code, whatever.
pub(crate) enum ResponseType {
ToTaskNum(usize),
/// Returns just the history from the WFT completed of the provided task number - 1, through to
/// the next WFT started. Simulating the incremental history for just the provided task number
#[from(ignore)]
OneTask(usize),
/// Waits until the future resolves before responding as `ToTaskNum` with the provided number
UntilResolved(BoxFuture<'static, ()>, usize),
/// Waits until the future resolves before responding with the provided response
UntilResolvedRaw(BoxFuture<'static, ()>, PollWorkflowTaskQueueResponse),
AllHistory,
Raw(PollWorkflowTaskQueueResponse),
}
#[derive(Eq, PartialEq, Hash)]
pub(crate) enum HashableResponseType {
ToTaskNum(usize),
OneTask(usize),
UntilResolved(usize),
UntilResolvedRaw(TaskToken),
AllHistory,
Raw(TaskToken),
}
impl ResponseType {
fn hashable(&self) -> HashableResponseType {
match self {
ResponseType::ToTaskNum(x) => HashableResponseType::ToTaskNum(*x),
ResponseType::OneTask(x) => HashableResponseType::OneTask(*x),
ResponseType::AllHistory => HashableResponseType::AllHistory,
ResponseType::Raw(r) => HashableResponseType::Raw(r.task_token.clone().into()),
ResponseType::UntilResolved(_, x) => HashableResponseType::UntilResolved(*x),
ResponseType::UntilResolvedRaw(_, r) => {
HashableResponseType::UntilResolvedRaw(r.task_token.clone().into())
}
}
}
}
impl From<&usize> for ResponseType {
fn from(u: &usize) -> Self {
Self::ToTaskNum(*u)
}
}
/// Given identifiers for a workflow/run, and a test history builder, construct an instance of
/// the a worker with a mock server client that will produce the responses as appropriate.
///
/// `response_batches` is used to control the fake [PollWorkflowTaskQueueResponse]s returned. For
/// each number in the input list, a fake response will be prepared which includes history up to the
/// workflow task with that number, as in [TestHistoryBuilder::get_history_info].
pub(crate) fn build_fake_worker(
wf_id: &str,
t: TestHistoryBuilder,
response_batches: impl IntoIterator<Item = impl Into<ResponseType>>,
) -> Worker {
let response_batches = response_batches.into_iter().map(Into::into).collect();
let mock_holder = build_multihist_mock_sg(
vec![FakeWfResponses {
wf_id: wf_id.to_owned(),
hist: t,
response_batches,
}],
true,
0,
);
mock_worker(mock_holder)
}
pub(crate) fn build_fake_sdk(mock_cfg: MockPollCfg) -> temporal_sdk::Worker {
let mut mock = build_mock_pollers(mock_cfg);
mock.worker_cfg(|c| {
c.max_cached_workflows = 1;
c.ignore_evicts_on_shutdown = false;
});
let core = mock_worker(mock);
let mut worker = temporal_sdk::Worker::new_from_core(Arc::new(core), "replay_q".to_string());
worker.set_worker_interceptor(FailOnNondeterminismInterceptor {});
worker
}
pub(crate) fn mock_worker(mocks: MocksHolder) -> Worker {
let sticky_q = sticky_q_name_for_worker("unit-test", &mocks.inputs.config);
let act_poller = if mocks.inputs.config.no_remote_activities {
None
} else {
mocks.inputs.act_poller
};
Worker::new_with_pollers(
mocks.inputs.config,
sticky_q,
mocks.client,
TaskPollers::Mocked {
wft_stream: mocks.inputs.wft_stream,
act_poller,
nexus_poller: mocks
.inputs
.nexus_poller
.unwrap_or_else(|| mock_poller_from_resps([])),
},
None,
)
}
pub(crate) fn mock_sdk(poll_cfg: MockPollCfg) -> TestWorker {
mock_sdk_cfg(poll_cfg, |_| {})
}
pub(crate) fn mock_sdk_cfg(
mut poll_cfg: MockPollCfg,
mutator: impl FnOnce(&mut WorkerConfig),
) -> TestWorker {
poll_cfg.using_rust_sdk = true;
let mut mock = build_mock_pollers(poll_cfg);
mock.worker_cfg(mutator);
let core = mock_worker(mock);
TestWorker::new(Arc::new(core), TEST_Q.to_string())
}
pub(crate) struct FakeWfResponses {
pub(crate) wf_id: String,
pub(crate) hist: TestHistoryBuilder,
pub(crate) response_batches: Vec<ResponseType>,
}
// TODO: Should be all-internal to this module
pub(crate) struct MocksHolder {
client: Arc<dyn WorkerClient>,
inputs: MockWorkerInputs,
pub(crate) outstanding_task_map: Option<OutstandingWFTMap>,
}
impl MocksHolder {
pub(crate) fn worker_cfg(&mut self, mutator: impl FnOnce(&mut WorkerConfig)) {
mutator(&mut self.inputs.config);
}
pub(crate) fn set_act_poller(&mut self, poller: BoxedPoller<PollActivityTaskQueueResponse>) {
self.inputs.act_poller = Some(poller);
}
/// Can be used for tests that need to avoid auto-shutdown due to running out of mock responses
pub(crate) fn make_wft_stream_interminable(&mut self) {
let old_stream = std::mem::replace(&mut self.inputs.wft_stream, stream::pending().boxed());
self.inputs.wft_stream = old_stream.chain(stream::pending()).boxed();
}
}
pub(crate) struct MockWorkerInputs {
pub(crate) wft_stream: BoxStream<'static, Result<ValidPollWFTQResponse, tonic::Status>>,
pub(crate) act_poller: Option<BoxedPoller<PollActivityTaskQueueResponse>>,
pub(crate) nexus_poller: Option<BoxedPoller<PollNexusTaskQueueResponse>>,
pub(crate) config: WorkerConfig,
}
impl Default for MockWorkerInputs {
fn default() -> Self {
Self::new(stream::pending().boxed())
}
}
impl MockWorkerInputs {
pub(crate) fn new(
wft_stream: BoxStream<'static, Result<ValidPollWFTQResponse, tonic::Status>>,
) -> Self {
Self {
wft_stream,
act_poller: None,
nexus_poller: None,
config: test_worker_cfg().build().unwrap(),
}
}
}
impl MocksHolder {
pub(crate) fn from_mock_worker(
client: impl WorkerClient + 'static,
mock_worker: MockWorkerInputs,
) -> Self {
Self {
client: Arc::new(client),
inputs: mock_worker,
outstanding_task_map: None,
}
}
/// Uses the provided list of tasks to create a mock poller for the `TEST_Q`
pub(crate) fn from_client_with_activities<ACT>(
client: impl WorkerClient + 'static,
act_tasks: ACT,
) -> Self
where
ACT: IntoIterator<Item = QueueResponse<PollActivityTaskQueueResponse>>,
<ACT as IntoIterator>::IntoIter: Send + 'static,
{
let wft_stream = stream::pending().boxed();
let mock_act_poller = mock_poller_from_resps(act_tasks);
let mock_worker = MockWorkerInputs {
wft_stream,
act_poller: Some(mock_act_poller),
nexus_poller: None,
config: test_worker_cfg().build().unwrap(),
};
Self {
client: Arc::new(client),
inputs: mock_worker,
outstanding_task_map: None,
}
}
/// Uses the provided task responses and delivers them as quickly as possible when polled.
/// This is only useful to test buffering, as typically you do not want to pretend that
/// the server is delivering WFTs super fast for the same run.
pub(crate) fn from_wft_stream(
client: impl WorkerClient + 'static,
stream: impl Stream<Item = PollWorkflowTaskQueueResponse> + Send + 'static,
) -> Self {
let wft_stream = stream
.map(|r| Ok(r.try_into().expect("Mock responses must be valid work")))
.boxed();
let mock_worker = MockWorkerInputs {
wft_stream,
act_poller: None,
nexus_poller: None,
config: test_worker_cfg().build().unwrap(),
};
Self {
client: Arc::new(client),
inputs: mock_worker,
outstanding_task_map: None,
}
}
}
// TODO: Un-pub ideally
pub(crate) fn mock_poller_from_resps<T, I>(tasks: I) -> BoxedPoller<T>
where
T: Send + Sync + 'static,
I: IntoIterator<Item = QueueResponse<T>>,
<I as IntoIterator>::IntoIter: Send + 'static,
{
let mut mock_poller = mock_manual_poller();
let mut tasks = tasks.into_iter();
mock_poller.expect_poll().returning(move || {
if let Some(t) = tasks.next() {
async move {
if let Some(f) = t.delay_until {
f.await;
}
Some(Ok(t.resp))
}
.boxed()
} else {
async { None }.boxed()
}
});
Box::new(mock_poller) as BoxedPoller<T>
}
pub(crate) fn mock_poller<T>() -> MockPoller<T>
where
T: Send + Sync + 'static,
{
let mut mock_poller = MockPoller::new();
mock_poller.expect_shutdown_box().return_const(());
mock_poller.expect_notify_shutdown().return_const(());
mock_poller
}
pub(crate) fn mock_manual_poller<T>() -> MockManualPoller<T>
where
T: Send + Sync + 'static,
{
let mut mock_poller = MockManualPoller::new();
mock_poller
.expect_shutdown_box()
.returning(|| async {}.boxed());
mock_poller.expect_notify_shutdown().return_const(());
mock_poller
}
/// Build a mock server client capable of returning multiple different histories for different
/// workflows. It does so by tracking outstanding workflow tasks like is also happening in core
/// (which is unfortunately a bit redundant, we could provide hooks in core but that feels a little
/// nasty). If there is an outstanding task for a given workflow, new chunks of its history are not
/// returned. If there is not, the next batch of history is returned for any workflow without an
/// outstanding task. Outstanding tasks are cleared on completion, failure, or eviction.
///
/// `num_expected_fails` can be provided to set a specific number of expected failed workflow tasks
/// sent to the server.
pub(crate) fn build_multihist_mock_sg(
hists: impl IntoIterator<Item = FakeWfResponses>,
enforce_correct_number_of_polls: bool,
num_expected_fails: usize,
) -> MocksHolder {
let mh = MockPollCfg::new(
hists.into_iter().collect(),
enforce_correct_number_of_polls,
num_expected_fails,
);
build_mock_pollers(mh)
}
/// See [build_multihist_mock_sg] -- one history convenience version
pub(crate) fn single_hist_mock_sg(
wf_id: &str,
t: TestHistoryBuilder,
response_batches: impl IntoIterator<Item = impl Into<ResponseType>>,
mock_client: MockWorkerClient,
enforce_num_polls: bool,
) -> MocksHolder {
let mut mh = MockPollCfg::from_resp_batches(wf_id, t, response_batches, mock_client);
mh.enforce_correct_number_of_polls = enforce_num_polls;
build_mock_pollers(mh)
}
type WFTCompletionMockFn = dyn FnMut(&WorkflowTaskCompletion) -> Result<RespondWorkflowTaskCompletedResponse, tonic::Status>
+ Send;
#[allow(clippy::type_complexity)]
pub(crate) struct MockPollCfg {
pub(crate) hists: Vec<FakeWfResponses>,
pub(crate) enforce_correct_number_of_polls: bool,
pub(crate) num_expected_fails: usize,
pub(crate) num_expected_legacy_query_resps: usize,
pub(crate) mock_client: MockWorkerClient,
/// All calls to fail WFTs must match this predicate
pub(crate) expect_fail_wft_matcher:
Box<dyn Fn(&TaskToken, &WorkflowTaskFailedCause, &Option<Failure>) -> bool + Send>,
pub(crate) completion_mock_fn: Option<Box<WFTCompletionMockFn>>,
pub(crate) num_expected_completions: Option<TimesRange>,
/// If being used with the Rust SDK, this is set true. It ensures pollers will not error out
/// early with no work, since we cannot know the exact number of times polling will happen.
/// Instead, they will just block forever.
pub(crate) using_rust_sdk: bool,
pub(crate) make_poll_stream_interminable: bool,
}
impl MockPollCfg {
pub(crate) fn new(
hists: Vec<FakeWfResponses>,
enforce_correct_number_of_polls: bool,
num_expected_fails: usize,
) -> Self {
Self {
hists,
enforce_correct_number_of_polls,
num_expected_fails,
num_expected_legacy_query_resps: 0,
mock_client: mock_workflow_client(),
expect_fail_wft_matcher: Box::new(|_, _, _| true),
completion_mock_fn: None,
num_expected_completions: None,
using_rust_sdk: false,
make_poll_stream_interminable: false,
}
}
/// Builds a config which will hand out each WFT in the history builder one by one
pub(crate) fn from_hist_builder(t: TestHistoryBuilder) -> Self {
let full_hist_info = t.get_full_history_info().unwrap();
let tasks = 1..=full_hist_info.wf_task_count();
Self::from_resp_batches("fake_wf_id", t, tasks, mock_workflow_client())
}
pub(crate) fn from_resps(
t: TestHistoryBuilder,
resps: impl IntoIterator<Item = impl Into<ResponseType>>,
) -> Self {
Self::from_resp_batches("fake_wf_id", t, resps, mock_workflow_client())
}
pub(crate) fn from_resp_batches(
wf_id: &str,
t: TestHistoryBuilder,
resps: impl IntoIterator<Item = impl Into<ResponseType>>,
mock_client: MockWorkerClient,
) -> Self {
Self {
hists: vec![FakeWfResponses {
wf_id: wf_id.to_owned(),
hist: t,
response_batches: resps.into_iter().map(Into::into).collect(),
}],
enforce_correct_number_of_polls: true,
num_expected_fails: 0,
num_expected_legacy_query_resps: 0,
mock_client,
expect_fail_wft_matcher: Box::new(|_, _, _| true),
completion_mock_fn: None,
num_expected_completions: None,
using_rust_sdk: false,
make_poll_stream_interminable: false,
}
}
pub(crate) fn completion_asserts_from_expectations(
&mut self,
builder_fn: impl FnOnce(CompletionAssertsBuilder<'_>),
) {
let builder = CompletionAssertsBuilder {
dest: &mut self.completion_mock_fn,
assertions: Default::default(),
};
builder_fn(builder);
}
}
#[allow(clippy::type_complexity)]
pub(crate) struct CompletionAssertsBuilder<'a> {
dest: &'a mut Option<Box<WFTCompletionMockFn>>,
assertions: VecDeque<Box<dyn FnOnce(&WorkflowTaskCompletion) + Send>>,
}
impl CompletionAssertsBuilder<'_> {
pub(crate) fn then(
&mut self,
assert: impl FnOnce(&WorkflowTaskCompletion) + Send + 'static,
) -> &mut Self {
self.assertions.push_back(Box::new(assert));
self
}
}
impl Drop for CompletionAssertsBuilder<'_> {
fn drop(&mut self) {
let mut asserts = std::mem::take(&mut self.assertions);
*self.dest = Some(Box::new(move |wtc| {
if let Some(fun) = asserts.pop_front() {
fun(wtc);
}
Ok(Default::default())
}));
}
}
#[derive(Default, Clone)]
pub(crate) struct OutstandingWFTMap {
map: Arc<RwLock<BiMap<String, TaskToken>>>,
waker: Arc<Notify>,
all_work_delivered: Arc<AtomicBool>,
}
impl OutstandingWFTMap {
fn has_run(&self, run_id: &str) -> bool {
self.map.read().contains_left(run_id)
}
fn put_token(&self, run_id: String, token: TaskToken) {
self.map.write().insert(run_id, token);
}
fn release_token(&self, token: &TaskToken) {
self.map.write().remove_by_right(token);
self.waker.notify_one();
}
pub(crate) fn release_run(&self, run_id: &str) {
self.map.write().remove_by_left(run_id);
self.waker.notify_waiters();
}
pub(crate) fn all_work_delivered(&self) -> bool {
self.all_work_delivered.load(Ordering::Acquire)
}
}
struct EnsuresWorkDoneWFTStream {
inner: UnboundedReceiverStream<ValidPollWFTQResponse>,
all_work_was_completed: Arc<AtomicBool>,
}
impl Stream for EnsuresWorkDoneWFTStream {
type Item = ValidPollWFTQResponse;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.inner.poll_next_unpin(cx)
}
}
impl Drop for EnsuresWorkDoneWFTStream {
fn drop(&mut self) {
if !self.all_work_was_completed.load(Ordering::Acquire) && !std::thread::panicking() {
panic!("Not all workflow tasks were taken from mock!");
}
}
}
/// Given an iterable of fake responses, return the mocks & associated data to work with them
pub(crate) fn build_mock_pollers(mut cfg: MockPollCfg) -> MocksHolder {
let mut task_q_resps: BTreeMap<String, VecDeque<_>> = BTreeMap::new();
let all_work_delivered = if cfg.enforce_correct_number_of_polls && !cfg.using_rust_sdk {
Arc::new(AtomicBool::new(false))
} else {
Arc::new(AtomicBool::new(true))
};
let outstanding_wf_task_tokens = OutstandingWFTMap {
map: Arc::new(Default::default()),
waker: Arc::new(Default::default()),
all_work_delivered: all_work_delivered.clone(),
};
for hist in cfg.hists {
let full_hist_info = hist.hist.get_full_history_info().unwrap();
// Ensure no response batch is trying to return more tasks than the history contains
for respt in &hist.response_batches {
if let ResponseType::ToTaskNum(rb_wf_num) = respt {
assert!(
*rb_wf_num <= full_hist_info.wf_task_count(),
"Wf task count {} is not <= total task count {}",
rb_wf_num,
full_hist_info.wf_task_count()
);
}
}
// Convert history batches into poll responses, while also tracking how many times a given
// history has been returned so we can increment the associated attempt number on the WFT.
// NOTE: This is hard to use properly with the `AfterEveryReply` testing eviction mode.
// Such usages need a history different from other eviction modes which would include
// WFT timeouts or something to simulate the task getting dropped.
let mut attempts_at_task_num = HashMap::new();
let responses: Vec<_> = hist
.response_batches
.into_iter()
.map(|response| {
let cur_attempt = attempts_at_task_num.entry(response.hashable()).or_insert(1);
let mut r = hist_to_poll_resp(&hist.hist, hist.wf_id.clone(), response);
r.attempt = *cur_attempt;
*cur_attempt += 1;
r
})
.collect();
let tasks = VecDeque::from(responses);
task_q_resps.insert(hist.wf_id, tasks);
}
// The poller will return history from any workflow runs that do not have currently
// outstanding tasks.
let outstanding = outstanding_wf_task_tokens.clone();
let outstanding_wakeup = outstanding.waker.clone();
let (wft_tx, wft_rx) = unbounded_channel();
tokio::task::spawn(async move {
loop {
let mut resp = None;
let mut resp_iter = task_q_resps.iter_mut();
for (_, tasks) in &mut resp_iter {
// Must extract run id from a workflow task associated with this workflow
// TODO: Case where run id changes for same workflow id is not handled here
if let Some(t) = tasks.front() {
let rid = t.workflow_execution.as_ref().unwrap().run_id.clone();
if !outstanding.has_run(&rid) {
let t = tasks.pop_front().unwrap();
outstanding.put_token(rid, TaskToken(t.task_token.clone()));
resp = Some(t);
break;
}
}
}
let no_tasks_for_anyone = resp_iter.next().is_none();
if let Some(resp) = resp {
if let Some(d) = resp.delay_until {
d.await;
}
if wft_tx
.send(
resp.resp
.try_into()
.expect("Mock responses must be valid work"),
)
.is_err()
{
dbg!("Exiting mock WFT task because rcv half of stream was dropped");
break;
}
}
// No more work to do
if task_q_resps.values().all(|q| q.is_empty()) {
outstanding
.all_work_delivered
.store(true, Ordering::Release);
break;
}
if no_tasks_for_anyone {
tokio::select! {
_ = outstanding_wakeup.notified() => {}
_ = tokio::time::sleep(Duration::from_secs(60)) => {}
}
}
}
});
let mock_worker = MockWorkerInputs::new(
EnsuresWorkDoneWFTStream {
inner: UnboundedReceiverStream::new(wft_rx),
all_work_was_completed: all_work_delivered,
}
.map(Ok)
.boxed(),
);
let outstanding = outstanding_wf_task_tokens.clone();
let expect_completes = cfg.mock_client.expect_complete_workflow_task();
if let Some(range) = cfg.num_expected_completions {
expect_completes.times(range);
} else if cfg.completion_mock_fn.is_some() {
expect_completes.times(1..);
}
expect_completes.returning(move |comp| {
let r = if let Some(ass) = cfg.completion_mock_fn.as_mut() {
// tee hee
ass(&comp)
} else {
Ok(RespondWorkflowTaskCompletedResponse::default())
};
outstanding.release_token(&comp.task_token);
r
});
let outstanding = outstanding_wf_task_tokens.clone();
cfg.mock_client
.expect_fail_workflow_task()
.withf(cfg.expect_fail_wft_matcher)
.times::<TimesRange>(cfg.num_expected_fails.into())
.returning(move |tt, _, _| {
outstanding.release_token(&tt);
Ok(Default::default())
});
let outstanding = outstanding_wf_task_tokens.clone();
cfg.mock_client
.expect_respond_legacy_query()
.times::<TimesRange>(cfg.num_expected_legacy_query_resps.into())
.returning(move |tt, _| {
outstanding.release_token(&tt);
Ok(Default::default())
});
let mut mh = MocksHolder {
client: Arc::new(cfg.mock_client),
inputs: mock_worker,
outstanding_task_map: Some(outstanding_wf_task_tokens),
};
if cfg.make_poll_stream_interminable {
mh.make_wft_stream_interminable();
}
mh
}
pub(crate) struct QueueResponse<T> {
pub(crate) resp: T,
pub(crate) delay_until: Option<BoxFuture<'static, ()>>,
}
impl<T> From<T> for QueueResponse<T> {
fn from(resp: T) -> Self {
QueueResponse {
resp,
delay_until: None,
}
}
}
impl From<QueueResponse<PollWorkflowTaskQueueResponse>> for ResponseType {
fn from(qr: QueueResponse<PollWorkflowTaskQueueResponse>) -> Self {
if let Some(du) = qr.delay_until {
ResponseType::UntilResolvedRaw(du, qr.resp)
} else {
ResponseType::Raw(qr.resp)
}
}
}
impl<T> Deref for QueueResponse<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.resp
}
}
impl<T> DerefMut for QueueResponse<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.resp
}
}
impl<T> Debug for QueueResponse<T>
where
T: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.resp.fmt(f)
}
}
pub(crate) trait PollWFTRespExt {
/// Add an update request to the poll response, using the update name "update_fn" and no args.
/// Returns the inner request body.
fn add_update_request(
&mut self,
update_id: impl ToString,
after_event_id: i64,
) -> update::v1::Request;
}
impl PollWFTRespExt for PollWorkflowTaskQueueResponse {
fn add_update_request(
&mut self,
update_id: impl ToString,
after_event_id: i64,
) -> update::v1::Request {
let upd_req_body = update::v1::Request {
meta: Some(update::v1::Meta {
update_id: update_id.to_string(),
identity: "agent_id".to_string(),
}),
input: Some(update::v1::Input {
header: None,
name: "update_fn".to_string(),
args: None,
}),
};
self.messages.push(protocol::v1::Message {
id: format!("update-{}", update_id.to_string()),
protocol_instance_id: update_id.to_string(),
body: Some(
pack_any(
"type.googleapis.com/temporal.api.update.v1.Request".to_string(),
&upd_req_body,
)
.unwrap(),
),
sequencing_id: Some(message::SequencingId::EventId(after_event_id)),
});
upd_req_body
}
}
pub(crate) fn hist_to_poll_resp(
t: &TestHistoryBuilder,
wf_id: impl Into<String>,
response_type: ResponseType,
) -> QueueResponse<PollWorkflowTaskQueueResponse> {
let run_id = t.get_orig_run_id();
let wf = WorkflowExecution {
workflow_id: wf_id.into(),
run_id: run_id.to_string(),
};
let mut delay_until = None;
let hist_info = match response_type {
ResponseType::ToTaskNum(tn) => t.get_history_info(tn).unwrap(),
ResponseType::OneTask(tn) => t.get_one_wft(tn).unwrap(),
ResponseType::AllHistory => t.get_full_history_info().unwrap(),
ResponseType::Raw(r) => {
return QueueResponse {
resp: r,
delay_until: None,
};
}
ResponseType::UntilResolved(fut, tn) => {
delay_until = Some(fut);
t.get_history_info(tn).unwrap()
}
ResponseType::UntilResolvedRaw(fut, r) => {
return QueueResponse {
resp: r,
delay_until: Some(fut),
};
}
};
let mut resp = hist_info.as_poll_wft_response();
resp.workflow_execution = Some(wf);
QueueResponse { resp, delay_until }
}
type AsserterWithReply<'a> = (
&'a dyn Fn(&WorkflowActivation),
workflow_activation_completion::Status,
);
/// Determines when workflows are kept in the cache or evicted for [poll_and_reply] type tests
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum WorkflowCachingPolicy {
/// Workflows are evicted after each workflow task completion. Note that this is *not* after
/// each workflow activation - there are often multiple activations per workflow task.
NonSticky,
/// Not a real mode, but good for imitating crashes. Evict workflows after *every* reply,
/// even if there are pending activations
#[cfg(test)]
AfterEveryReply,
}
/// This function accepts a list of asserts and replies to workflow activations to run against the
/// provided instance of fake core.
///
/// It handles the business of re-sending the same activation replies over again in the event
/// of eviction or workflow activation failure. Activation failures specifically are only run once,
/// since they clearly can't be returned every time we replay the workflow, or it could never
/// proceed
pub(crate) async fn poll_and_reply<'a>(
worker: &'a Worker,
eviction_mode: WorkflowCachingPolicy,
expect_and_reply: &'a [AsserterWithReply<'a>],
) {
poll_and_reply_clears_outstanding_evicts(worker, None, eviction_mode, expect_and_reply).await;
}
pub(crate) async fn poll_and_reply_clears_outstanding_evicts<'a>(
worker: &'a Worker,
outstanding_map: Option<OutstandingWFTMap>,
eviction_mode: WorkflowCachingPolicy,
expect_and_reply: &'a [AsserterWithReply<'a>],
) {
let mut evictions = 0;
let expected_evictions = expect_and_reply.len() - 1;
let mut executed_failures = HashSet::new();
let expected_fail_count = expect_and_reply
.iter()
.filter(|(_, reply)| !reply.is_success())
.count();
'outer: loop {
let expect_iter = expect_and_reply.iter();
for (i, interaction) in expect_iter.enumerate() {
let (asserter, reply) = interaction;
let complete_is_failure = !reply.is_success();
// Only send activation failures once
if executed_failures.contains(&i) {
continue;
}
let mut res = worker.poll_workflow_activation().await.unwrap();
if res.jobs.iter().any(|j| {
matches!(
j.variant,
Some(workflow_activation_job::Variant::RemoveFromCache(_))
)
}) && res.jobs.len() > 1
{
panic!("Saw an activation with an eviction & other work! {res:?}");
}
let is_eviction = res.is_only_eviction();
let mut do_release = false;
if is_eviction {
// If the job is an eviction, clear it, since in the tests we don't explicitly
// specify evict assertions
res.jobs.clear();
do_release = true;
}
// TODO: Can remove this if?
if !res.jobs.is_empty() {
asserter(&res);
}
let reply = if res.jobs.is_empty() {
// Just an eviction
WorkflowActivationCompletion::empty(res.run_id.clone())
} else {
// Eviction plus some work, we still want to issue the reply
WorkflowActivationCompletion {
run_id: res.run_id.clone(),
status: Some(reply.clone()),
}
};
let ends_execution = reply.has_execution_ending();
worker.complete_workflow_activation(reply).await.unwrap();
if do_release {
if let Some(omap) = outstanding_map.as_ref() {
omap.release_run(&res.run_id);
}
}
// Restart assertions from the beginning if it was an eviction (and workflow execution
// isn't over)
if is_eviction && !ends_execution {
continue 'outer;
}
if complete_is_failure {
executed_failures.insert(i);
}
match eviction_mode {
WorkflowCachingPolicy::NonSticky => (),
WorkflowCachingPolicy::AfterEveryReply => {
if evictions < expected_evictions {
worker.request_workflow_eviction(&res.run_id);
evictions += 1;
}
}
}
}
break;
}
assert_eq!(expected_fail_count, executed_failures.len());
assert_eq!(worker.outstanding_workflow_tasks().await, 0);
}
pub(crate) fn gen_assert_and_reply(
asserter: &dyn Fn(&WorkflowActivation),
reply_commands: Vec<workflow_command::Variant>,
) -> AsserterWithReply<'_> {
(
asserter,
workflow_completion::Success::from_variants(reply_commands).into(),
)
}
pub(crate) fn gen_assert_and_fail(asserter: &dyn Fn(&WorkflowActivation)) -> AsserterWithReply<'_> {
(
asserter,
workflow_completion::Failure {
failure: Some(Failure {
message: "Intentional test failure".to_string(),
..Default::default()