• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lucaslorentz / durabletask-extensions / 5835581151

pending completion
5835581151

Pull #27

github

lucaslorentz
Add ExtendedOrchestrationContext to simplify workers API
Pull Request #27: Add ExtendedOrchestrationContext to simplify workers API

71 of 71 new or added lines in 5 files covered. (100.0%)

2290 of 2798 relevant lines covered (81.84%)

142.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.14
/src/LLL.DurableTask.Worker/OrchestrationContextEventExtensions.cs
1
using System;
2
using System.Threading;
3
using System.Threading.Tasks;
4

5
namespace LLL.DurableTask.Worker
6
{
7
    public static class OrchestrationContextEventExtensions
8
    {
9
        public static async Task<T> WaitForEventAsync<T>(
10
            this ExtendedOrchestrationContext context,
11
            string eventType,
12
            TimeSpan timeout,
13
            T defaultValue,
14
            CancellationToken cancellationToken = default)
15
        {
16
            using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
2✔
17
            var timerTask = context.CreateTimer<object>(context.CurrentUtcDateTime.Add(timeout), null, cts.Token);
2✔
18
            var eventTask = context.WaitForEventAsync<T>(eventType, cts.Token);
2✔
19
            var winningTask = await Task.WhenAny(timerTask, eventTask);
2✔
20
            cts.Cancel();
2✔
21
            return timerTask == winningTask
2✔
22
                ? defaultValue
2✔
23
                : await eventTask;
2✔
24
        }
25

26
        public static async Task<T> WaitForEventAsync<T>(
27
            this ExtendedOrchestrationContext context,
28
            string eventType,
29
            CancellationToken cancellationToken = default)
30
        {
31
            var taskCompletionSource = new TaskCompletionSource<T>();
8✔
32

33
            using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled(cancellationToken)))
10✔
34
            {
35
                using (context.AddEventListener<T>(eventType, taskCompletionSource.SetResult, taskCompletionSource.SetException))
8✔
36
                {
37
                    return await taskCompletionSource.Task;
8✔
38
                }
39
            }
40
        }
41

42
        public static IDisposable AddEventListener<T>(
43
            this ExtendedOrchestrationContext context,
44
            string eventType,
45
            Action<T> handler,
46
            Action<Exception> exceptionHandler = null)
47
        {
48
            return new OrchestrationEventListener(context, (type, input) =>
12✔
49
            {
12✔
50
                if (type == eventType)
46✔
51
                {
12✔
52
                    try
12✔
53
                    {
12✔
54
                        handler(context.MessageDataConverter.Deserialize<T>(input));
26✔
55
                    }
12✔
56
                    catch (Exception ex)
12✔
57
                    {
12✔
58
                        exceptionHandler?.Invoke(ex);
12✔
59
                    }
12✔
60
                }
12✔
61
            });
12✔
62
        }
63

64
        public static IDisposable AddEventListener(
65
            this ExtendedOrchestrationContext context,
66
            Action<string, string> handler)
67
        {
68
            return new OrchestrationEventListener(context, handler);
×
69
        }
70

71
        private class OrchestrationEventListener : IDisposable
72
        {
73
            private readonly ExtendedOrchestrationContext _context;
74
            private readonly Action<string, string> _handler;
75

76
            public OrchestrationEventListener(
12✔
77
                ExtendedOrchestrationContext context,
12✔
78
                Action<string, string> handler)
12✔
79
            {
80
                _context = context;
12✔
81
                _handler = handler;
12✔
82
                _context.Event += _handler;
12✔
83
            }
84

85
            public void Dispose()
86
            {
87
                GC.SuppressFinalize(this);
12✔
88
                _context.Event -= _handler;
12✔
89
            }
90
        }
91
    }
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc