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

realm / realm-dotnet / 9661237267

25 Jun 2024 11:02AM UTC coverage: 81.358% (+0.09%) from 81.271%
9661237267

push

github

web-flow
RNET-1137: Fix progress notification (#3615)

* Corrected source generation

* Removed unused values

* Small fix

* Fixed test

* Fixed basic test

* Added changelog

* Fixed changelog

* Moved progress saving

2307 of 2987 branches covered (77.23%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

121 existing lines in 5 files now uncovered.

6832 of 8246 relevant lines covered (82.85%)

42331.92 hits per line

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

0.0
/Realm/Realm/Native/SyncSocketProvider.EventLoop.cs
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2023 Realm Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
using System;
20
using System.Threading;
21
using System.Threading.Channels;
22
using System.Threading.Tasks;
23
using Realms.Logging;
24

25
namespace Realms.Native;
26

27
internal partial class SyncSocketProvider
28
{
29
    private class Timer
30
    {
UNCOV
31
        private readonly CancellationTokenSource _cts = new();
×
32

UNCOV
33
        internal Timer(TimeSpan delay, IntPtr nativeCallback, ChannelWriter<IWork> workQueue)
×
34
        {
UNCOV
35
            Logger.LogDefault(LogLevel.Trace, $"Creating timer with delay {delay} and target {nativeCallback}.");
×
36
            var cancellationToken = _cts.Token;
×
37
            Task.Delay(delay, cancellationToken).ContinueWith(async _ =>
×
38
            {
×
39
                await workQueue.WriteAsync(new Work(nativeCallback, cancellationToken));
×
40
            });
×
41
        }
×
42

43
        internal void Cancel()
44
        {
UNCOV
45
            Logger.LogDefault(LogLevel.Trace, $"Canceling timer.");
×
46
            _cts.Cancel();
×
47
            _cts.Dispose();
×
48
        }
×
49

UNCOV
50
        private class Work(IntPtr nativeCallback, CancellationToken cancellationToken)
×
51
            : IWork
52
        {
53
            public void Execute()
54
            {
UNCOV
55
                var status = Status.OK;
×
56
                if (cancellationToken.IsCancellationRequested)
×
57
                {
58
                    status = new(ErrorCode.OperationAborted, "Timer canceled");
×
59
                }
60

UNCOV
61
                RunCallback(nativeCallback, status);
×
UNCOV
62
            }
×
63
        }
64
    }
65

66
    // Belongs to SyncSocketProvider. When Native destroys the Provider we need to stop executing
67
    // enqueued work, but we need to release all the callbacks we copied on the heap.
UNCOV
68
    private class EventLoopWork(IntPtr nativeCallback, CancellationToken cancellationToken)
×
69
        : IWork
70
    {
71
        public void Execute()
72
        {
UNCOV
73
            if (cancellationToken.IsCancellationRequested)
×
74
            {
UNCOV
75
                Logger.LogDefault(LogLevel.Trace, "Deleting EventLoopWork callback only because event loop was cancelled.");
×
UNCOV
76
                NativeMethods.delete_callback(nativeCallback);
×
UNCOV
77
                return;
×
78
            }
79

UNCOV
80
            RunCallback(nativeCallback, Status.OK);
×
UNCOV
81
        }
×
82
    }
83

84
    private static void RunCallback(IntPtr nativeCallback, Status status)
85
    {
86
        Logger.LogDefault(LogLevel.Trace, $"SyncSocketProvider running native callback {nativeCallback} with status {status.Code} \"{status.Reason}\".");
×
87

88
        using var arena = new Arena();
×
89
        NativeMethods.run_callback(nativeCallback, status.Code, StringValue.AllocateFrom(status.Reason, arena));
×
UNCOV
90
    }
×
91

92
    private async Task PostWorkAsync(IntPtr nativeCallback)
93
    {
UNCOV
94
        Logger.LogDefault(LogLevel.Trace, "Posting work to SyncSocketProvider event loop.");
×
95
        await _workQueue.Writer.WriteAsync(new EventLoopWork(nativeCallback, _cts.Token));
×
96
    }
×
97

98
    private async partial Task WorkThread()
99
    {
100
        Logger.LogDefault(LogLevel.Trace, "Starting SyncSocketProvider event loop.");
×
101
        try
102
        {
UNCOV
103
            while (await _workQueue.Reader.WaitToReadAsync())
×
104
            {
UNCOV
105
                while (_workQueue.Reader.TryRead(out var work))
×
106
                {
UNCOV
107
                    work.Execute();
×
108
                }
×
109
            }
110
        }
×
UNCOV
111
        catch (Exception e)
×
112
        {
UNCOV
113
            Logger.LogDefault(LogLevel.Error, $"Error occurred in SyncSocketProvider event loop {e.GetType().FullName}: {e.Message}");
×
114
            if (!string.IsNullOrEmpty(e.StackTrace))
×
115
            {
116
                Logger.LogDefault(LogLevel.Trace, e.StackTrace);
×
117
            }
118

UNCOV
119
            throw;
×
120
        }
121

UNCOV
122
        Logger.LogDefault(LogLevel.Trace, "Exiting SyncSocketProvider event loop.");
×
123
    }
×
124
}
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

© 2026 Coveralls, Inc