/// <summary>
        /// Create an analyzer driver.
        /// </summary>
        /// <param name="analyzers">The set of analyzers to include in the analysis</param>
        /// <param name="options">Options that are passed to analyzers</param>
        /// <param name="cancellationToken">a cancellation token that can be used to abort analysis</param>
        /// <param name="continueOnAnalyzerException">Delegate which is invoked when an analyzer throws an exception.
        /// If a non-null delegate is provided and it returns true, then the exception is handled and converted into a diagnostic and driver continues with other analyzers.
        /// Otherwise if it returns false, then the exception is not handled by the driver.
        /// If null, then the driver always handles the exception.
        /// </param>
        protected AnalyzerDriver(ImmutableArray<IDiagnosticAnalyzer> analyzers, AnalyzerOptions options, CancellationToken cancellationToken, Func<Exception, IDiagnosticAnalyzer, bool> continueOnAnalyzerException = null)
        {
            this.CompilationEventQueue = new AsyncQueue<CompilationEvent>();
            this.DiagnosticQueue = new AsyncQueue<Diagnostic>();
            this.addDiagnostic = GetDiagnosticSinkWithSuppression();
            this.analyzerOptions = options;

            Func<Exception, IDiagnosticAnalyzer, bool> defaultExceptionHandler = (exception, analyzer) => true;
            this.continueOnAnalyzerException = continueOnAnalyzerException ?? defaultExceptionHandler;

            // start the first task to drain the event queue. The first compilation event is to be handled before
            // any other ones, so we cannot have more than one event processing task until the first event has been handled.
            initialWorker = Task.Run(async () =>
            {
                try
                {
                    await InitialWorkerAsync(analyzers, continueOnAnalyzerException, cancellationToken).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    // If creation is cancelled we had better not use the driver any longer
                    this.Dispose();
                }
            });
        }
 public void TryEnqueueAfterComplete()
 {
     var queue = new AsyncQueue<int>();
     Assert.True(queue.TryEnqueue(42));
     queue.Complete();
     Assert.False(queue.TryEnqueue(42));
 }
        public void Dequeue_should_return_an_uncompleted_task_when_no_items_exist()
        {
            var subject = new AsyncQueue<int>();
            var result = subject.DequeueAsync(CancellationToken.None);

            result.IsCompleted.Should().BeFalse();
        }
        // constructors
        public MultiServerCluster(ClusterSettings settings, IClusterableServerFactory serverFactory, IEventSubscriber eventSubscriber)
            : base(settings, serverFactory, eventSubscriber)
        {
            Ensure.IsGreaterThanZero(settings.EndPoints.Count, "settings.EndPoints.Count");
            if (settings.ConnectionMode == ClusterConnectionMode.Standalone)
            {
                throw new ArgumentException("ClusterConnectionMode.StandAlone is not supported for a MultiServerCluster.");
            }
            if (settings.ConnectionMode == ClusterConnectionMode.Direct)
            {
                throw new ArgumentException("ClusterConnectionMode.Direct is not supported for a MultiServerCluster.");
            }

            _monitorServersCancellationTokenSource = new CancellationTokenSource();
            _serverDescriptionChangedQueue = new AsyncQueue<ServerDescriptionChangedEventArgs>();
            _servers = new List<IClusterableServer>();
            _state = new InterlockedInt32(State.Initial);
            _replicaSetName = settings.ReplicaSetName;

            eventSubscriber.TryGetEventHandler(out _closingEventHandler);
            eventSubscriber.TryGetEventHandler(out _closedEventHandler);
            eventSubscriber.TryGetEventHandler(out _openingEventHandler);
            eventSubscriber.TryGetEventHandler(out _openedEventHandler);
            eventSubscriber.TryGetEventHandler(out _addingServerEventHandler);
            eventSubscriber.TryGetEventHandler(out _addedServerEventHandler);
            eventSubscriber.TryGetEventHandler(out _removingServerEventHandler);
            eventSubscriber.TryGetEventHandler(out _removedServerEventHandler);
        }
 public void DequeueAsync_before_Enqueue()
 {
     var queue = new AsyncQueue<string>();
     var task = queue.DequeueAsync().AssertNotCompleted();
     queue.Enqueue("A");
     task.AssertResult("A");
 }
 public void Enqueue()
 {
     var queue = new AsyncQueue<int>();
     queue.Enqueue(42);
     var task = queue.DequeueAsync();
     Assert.True(task.IsCompleted);
     Assert.Equal(42, task.Result);
 }
        public void Dequeue_should_complete_when_an_item_is_added_to_the_queue()
        {
            var subject = new AsyncQueue<int>();
            var result = subject.DequeueAsync(CancellationToken.None);
            subject.Enqueue(10);

            result.Result.Should().Be(10);
        }
 public async Task DequeueThenEnqueue()
 {
     var queue = new AsyncQueue<int>();
     var task = queue.DequeueAsync();
     Assert.False(task.IsCompleted);
     queue.Enqueue(13);
     Assert.Equal(13, await task.ConfigureAwait(false));
 }
Exemple #9
0
        /// <summary>
        /// Creates a new instance of the <see cref="AlarmAdapter"/>.
        /// </summary>
        public AlarmAdapter()
        {
            m_measurementQueue = new AsyncQueue<IEnumerable<IMeasurement>>()
            {
                ProcessItemFunction = ProcessMeasurements
            };

            m_measurementQueue.ProcessException += m_measurementQueue_ProcessException;
        }
        internal static void VerifyEvents(AsyncQueue<CompilationEvent> queue, params string[] expectedEvents)
        {
            var expected = new HashSet<string>();
            foreach (var s in expectedEvents)
            {
                if (!expected.Add(s))
                {
                    Console.WriteLine("Expected duplicate " + s);
                }
            }

            var actual = ArrayBuilder<CompilationEvent>.GetInstance();
            while (queue.Count > 0 || !queue.IsCompleted)
            {
                var te = queue.DequeueAsync();
                Assert.True(te.IsCompleted);
                actual.Add(te.Result);
            }
            bool unexpected = false;
            foreach (var a in actual)
            {
                var eventString = a.ToString();
                if (!expected.Remove(eventString))
                {
                    if (!unexpected)
                    {
                        Console.WriteLine("UNEXPECTED EVENTS:");
                        unexpected = true;
                    }
                    Console.WriteLine(eventString);
                }
            }
            if (expected.Count != 0)
            {
                Console.WriteLine("MISSING EVENTS:");
            }
            foreach (var e in expected)
            {
                Console.WriteLine(e);
            }
            if (unexpected || expected.Count != 0)
            {
                bool first = true;
                Console.WriteLine("ACTUAL EVENTS:");
                foreach (var e in actual)
                {
                    if (!first)
                    {
                        Console.WriteLine(",");
                    }
                    first = false;
                    Console.Write("\"" + e.ToString() + "\"");
                }
                Console.WriteLine();
                Assert.True(false);
            }
        }
Exemple #11
0
 public void TryEnqueueAfterPromisingNotTo()
 {
     var queue = new AsyncQueue<int>();
     Assert.True(queue.TryEnqueue(42));
     queue.PromiseNotToEnqueue();
     Assert.Throws(typeof(InvalidOperationException), () => {
         queue.TryEnqueue(42);
     });
 }
 public void Count_nonempty()
 {
     var queue = new AsyncQueue<string>();
     queue.Enqueue("A");
     queue.Enqueue("B");
     queue.Enqueue("C");
     queue.Enqueue("D");
     Assert.IsTrue(queue.TryDequeue());
     Assert.AreEqual(3, queue.Count);
 }
 public void CancelAllDequeue()
 {
     var queue = new AsyncQueue<string>();
     var tasks = Enumerable.Range(0, 3).Select(_ => queue.DequeueAsync()).ToList();
     queue.CancelAllDequeue().Dispose();
     foreach (var task in tasks)
     {
         task.AssertCanceled();
     }
 }
 public void CompleteAllDequeue()
 {
     var queue = new AsyncQueue<string>();
     var tasks = Enumerable.Range(0, 3).Select(_ => queue.DequeueAsync()).ToList();
     queue.CompleteAllDequeue("X").Dispose();
     foreach (var task in tasks)
     {
         task.AssertResult("X");
     }
 }
 public void DequeueAsync_canceled_after_completion()
 {
     var queue = new AsyncQueue<string>();
     queue.Enqueue("A");
     using (var cts = new CancellationTokenSource())
     {
         var task = queue.DequeueAsync(cts.Token);
         cts.Cancel();
         task.AssertResult("A");
     }
 }
Exemple #16
0
        public void Items_should_be_dequeued_in_the_order_they_were_enqueued_2()
        {
            var subject = new AsyncQueue<int>();
            var result = subject.DequeueAsync(CancellationToken.None);
            var result2 = subject.DequeueAsync(CancellationToken.None);
            subject.Enqueue(10);
            subject.Enqueue(11);

            result.Result.Should().Be(10);
            result2.Result.Should().Be(11);
        }
 private async Task ConsumeItems(
     AsyncQueue<int> inputQueue,
     ConcurrentBag<int> outputItems,
     CancellationToken cancellationToken)
 {
     while (!cancellationToken.IsCancellationRequested)
     {
         int consumedItem = await inputQueue.DequeueAsync(cancellationToken);
         outputItems.Add(consumedItem);
     }
 }
        // constructors
        public MultiServerCluster(ClusterSettings settings, IClusterableServerFactory serverFactory, IClusterListener listener)
            : base(settings, serverFactory, listener)
        {
            Ensure.IsGreaterThanZero(settings.EndPoints.Count, "settings.EndPoints.Count");

            _monitorServersCancellationTokenSource = new CancellationTokenSource();
            _serverDescriptionChangedQueue = new AsyncQueue<ServerDescriptionChangedEventArgs>();
            _servers = new List<IClusterableServer>();
            _state = new InterlockedInt32(State.Initial);
            _replicaSetName = settings.ReplicaSetName;
        }
Exemple #19
0
        public void DequeueAll_should_dequeue_all_items(int n)
        {
            var subject = new AsyncQueue<int>();
            for (var i = 0; i < n; i++)
            {
                subject.Enqueue(i);
            }

            var count = subject.DequeueAll().Count();
            count.Should().Be(n);
        }
Exemple #20
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MetricImporter"/> class.
        /// </summary>
        public MetricImporter()
        {
            m_fileProcessQueue = new AsyncQueue<string>();
            m_fileProcessQueue.ProcessItemFunction = ProcessFile;
            m_fileProcessQueue.ProcessException += m_fileProcessQueue_ProcessException;

            m_columns = new Dictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
            m_columnMappings = new Dictionary<int, IMeasurement>();

            m_inputInterval = 5.0D;
            m_measurementsPerInterval = 5;
            m_skipRows = 4;
            m_timestampFormat = "dd-MMM-yyyy HH:mm:ss.fff";

            // Set minimum timer resolution to one millisecond to improve timer accuracy
            PrecisionTimer.SetMinimumTimerResolution(1);
        }
Exemple #21
0
        public void TestAssignActor_RegistersActorHandlers()
        {
            var actorRegistrationsQueue = new AsyncQueue<IActor>();

            var actorHost = new ActorHost(new SocketFactory(null),
                                          actorHandlersMap,
                                          new AsyncQueue<AsyncMessageContext>(),
                                          actorRegistrationsQueue,
                                          routerConfiguration,
                                          messageTracer.Object,
                                          logger);
            actorHost.AssignActor(new EchoActor());

            var registration = actorRegistrationsQueue.GetConsumingEnumerable(CancellationToken.None).First();
            Assert.IsTrue(registration.GetInterfaceDefinition().Any(id => id.Message.Identity == SimpleMessage.MessageIdentity));
            Assert.IsTrue(registration.GetInterfaceDefinition().Any(id => id.Message.Version == Message.CurrentVersion));
        }
        public async Task AsyncQueueSkipsCancelledTasks()
        {
            AsyncQueue<int> inputQueue = new AsyncQueue<int>();

            // Queue up a couple of tasks to wait for input
            CancellationTokenSource cancellationSource = new CancellationTokenSource();
            Task<int> taskOne = inputQueue.DequeueAsync(cancellationSource.Token);
            Task<int> taskTwo = inputQueue.DequeueAsync();

            // Cancel the first task and then enqueue a number
            cancellationSource.Cancel();
            await inputQueue.EnqueueAsync(1);

            // Did the second task get the number?
            Assert.Equal(TaskStatus.Canceled, taskOne.Status);
            Assert.Equal(TaskStatus.RanToCompletion, taskTwo.Status);
            Assert.Equal(1, taskTwo.Result);
        }
        public async Task DequeueThenComplete()
        {
            var queue = new AsyncQueue<int>();
            var task = queue.DequeueAsync();
            Assert.False(task.IsCompleted);

            queue.Complete();
            var threw = false;
            try
            {
                await task.ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                threw = true;
            }

            Assert.True(threw);
        }
        public async Task DequeueManyThenEnqueueMany()
        {
            var queue = new AsyncQueue<int>();
            var count = 4;
            var list = new List<Task<int>>();

            for (var i = 0; i < count; i++)
            {
                list.Add(queue.DequeueAsync());
            }

            for (var i = 0; i < count; i++)
            {
                var task = list[i];
                Assert.False(task.IsCompleted);
                queue.Enqueue(i);
                Assert.Equal(i, await task.ConfigureAwait(false));
            }
        }
Exemple #25
0
        async void RunBluetoothListener()
        {
            BluetoothListener listener;

            try
            {
                listener = new BluetoothListener(new Guid("{05e9b9dd-1688-4785-bb1d-1c750034157b}"));
                listener.Start();
            }
            catch
            {
                MessageBox.Show($"Unable to connect to bluetooth");
                return;
            }
            while (true)
            {
                var neoSocket = new NeoSocket(await Task.Run(() => listener.AcceptBluetoothClient()));
                var queue     = new AsyncQueue <byte[]>();
                Reader(neoSocket, queue);
                Writer(neoSocket, queue);
            }
        }
        public async Task AsyncQueueSynchronizesAccess()
        {
            ConcurrentBag<int> outputItems = new ConcurrentBag<int>();
            AsyncQueue<int> inputQueue = new AsyncQueue<int>(Enumerable.Range(0, 100));
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

            try
            {
                // Start 5 consumers
                await Task.WhenAll(
                    Task.Run(() => ConsumeItems(inputQueue, outputItems, cancellationTokenSource.Token)),
                    Task.Run(() => ConsumeItems(inputQueue, outputItems, cancellationTokenSource.Token)),
                    Task.Run(() => ConsumeItems(inputQueue, outputItems, cancellationTokenSource.Token)),
                    Task.Run(() => ConsumeItems(inputQueue, outputItems, cancellationTokenSource.Token)),
                    Task.Run(() => ConsumeItems(inputQueue, outputItems, cancellationTokenSource.Token)),
                    Task.Run(
                        async () =>
                        {
                            // Wait for a bit and then add more items to the queue
                            await Task.Delay(250);

                            foreach (var i in Enumerable.Range(100, 200))
                            {
                                await inputQueue.EnqueueAsync(i);
                            }

                            // Cancel the waiters
                            cancellationTokenSource.Cancel();
                        }));
            }
            catch (TaskCanceledException)
            {
                // Do nothing, this is expected.
            }

            // At this point, numbers 0 through 299 should be in the outputItems
            IEnumerable<int> expectedItems = Enumerable.Range(0, 300);
            Assert.Equal(0, expectedItems.Except(outputItems).Count());
        }
Exemple #27
0
        public void ReportHangOnRunAsyncThenJoin()
        {
            this.Factory.HangDetectionTimeout = TimeSpan.FromMilliseconds(10);
            var releaseTaskSource = new TaskCompletionSource <object>();
            var hangQueue         = new AsyncQueue <TimeSpan>();

            this.Context.OnReportHang = (hangDuration, iterations, id) =>
            {
                hangQueue.Enqueue(hangDuration);
            };

            Task.Run(async delegate
            {
                var ct = new CancellationTokenSource(TestTimeout).Token;
                try
                {
                    TimeSpan lastDuration = TimeSpan.Zero;
                    for (int i = 0; i < 3; i++)
                    {
                        var duration = await hangQueue.DequeueAsync(ct);
                        Assert.True(lastDuration == TimeSpan.Zero || lastDuration < duration);
                        lastDuration = duration;
                    }

                    releaseTaskSource.SetResult(null);
                }
                catch (Exception ex)
                {
                    releaseTaskSource.SetException(ex);
                }
            }).Forget();

            var joinableTask = this.Factory.RunAsync(async delegate
            {
                await releaseTaskSource.Task;
            });

            joinableTask.Join();
        }
        public BlockStoreSignaled(
            IBlockStoreQueue blockStoreQueue,
            ConcurrentChain chain,
            StoreSettings storeSettings,
            IChainState chainState,
            IConnectionManager connection,
            INodeLifetime nodeLifetime,
            ILoggerFactory loggerFactory,
            IInitialBlockDownloadState initialBlockDownloadState)
        {
            this.blockStoreQueue           = blockStoreQueue;
            this.chain                     = chain;
            this.chainState                = chainState;
            this.connection                = connection;
            this.nodeLifetime              = nodeLifetime;
            this.logger                    = loggerFactory.CreateLogger(this.GetType().FullName);
            this.storeSettings             = storeSettings;
            this.initialBlockDownloadState = initialBlockDownloadState;

            this.blocksToAnnounce = new AsyncQueue <ChainedHeader>();
            this.dequeueLoopTask  = this.DequeueContinuouslyAsync();
        }
Exemple #29
0
        public FederationWalletSyncManager(ILoggerFactory loggerFactory, IFederationWalletManager walletManager, ChainIndexer chain,
                                           Network network, IBlockStore blockStore, StoreSettings storeSettings, INodeLifetime nodeLifetime)
        {
            Guard.NotNull(loggerFactory, nameof(loggerFactory));
            Guard.NotNull(walletManager, nameof(walletManager));
            Guard.NotNull(chain, nameof(chain));
            Guard.NotNull(network, nameof(network));
            Guard.NotNull(blockStore, nameof(blockStore));
            Guard.NotNull(storeSettings, nameof(storeSettings));
            Guard.NotNull(nodeLifetime, nameof(nodeLifetime));

            this.walletManager = walletManager;
            this.chain         = chain;
            this.blockStore    = blockStore;
            this.coinType      = (CoinType)network.Consensus.CoinType;
            this.storeSettings = storeSettings;
            this.nodeLifetime  = nodeLifetime;
            this.logger        = loggerFactory.CreateLogger(this.GetType().FullName);
            this.blocksQueue   = new AsyncQueue <Block>(this.OnProcessBlockAsync);

            this.blocksQueueSize = 0;
        }
        public void TestQueuedSymbols()
        {
            var source =
                @"namespace N
{
  partial class C<T1>
  {
    partial void M(int x1);
    internal int P { get; private set; }
    int F = 12;
    void N<T2>(int y = 12) { F = F + 1; }
  }
  partial class C<T1>
  {
    partial void M(int x2) {}
  }
}";
            var q = new AsyncQueue <CompilationEvent>();

            CreateCompilationWithMscorlib45(source)
            .WithEventQueue(q)
            .VerifyDiagnostics()      // force diagnostics twice
            .VerifyDiagnostics();
            VerifyEvents(q,
                         "CompilationStartedEvent",
                         "SymbolDeclaredCompilationEvent(P int C<T1>.P @ : (5,4)-(5,40))",
                         "SymbolDeclaredCompilationEvent(F int C<T1>.F @ : (6,8)-(6,14))",
                         "SymbolDeclaredCompilationEvent(C C<T1> @ : (2,2)-(8,3), : (9,2)-(12,3))",
                         "SymbolDeclaredCompilationEvent(M void C<T1>.M(int x1) @ : (4,4)-(4,27))",
                         "SymbolDeclaredCompilationEvent(M void C<T1>.M(int x2) @ : (11,4)-(11,29))",
                         "SymbolDeclaredCompilationEvent(N N @ : (0,0)-(13,1))",
                         "SymbolDeclaredCompilationEvent(<empty>  @ : (0,0)-(13,1))",
                         "SymbolDeclaredCompilationEvent(get_P int C<T1>.P.get @ : (5,21)-(5,25))",
                         "SymbolDeclaredCompilationEvent(set_P void C<T1>.P.set @ : (5,26)-(5,38))",
                         "SymbolDeclaredCompilationEvent(N void C<T1>.N<T2>(int y = 12) @ : (7,4)-(7,41))",
                         "CompilationUnitCompletedEvent()",
                         "CompilationCompletedEvent"
                         );
        }
        public BlockStoreSignaled(
            BlockStoreLoop blockStoreLoop,
            ConcurrentChain chain,
            StoreSettings storeSettings,
            IChainState chainState,
            IConnectionManager connection,
            INodeLifetime nodeLifetime,
            ILoggerFactory loggerFactory,
            IBlockStoreCache blockStoreCache)
        {
            this.blockStoreLoop  = blockStoreLoop;
            this.chain           = chain;
            this.chainState      = chainState;
            this.connection      = connection;
            this.nodeLifetime    = nodeLifetime;
            this.logger          = loggerFactory.CreateLogger(this.GetType().FullName);
            this.storeSettings   = storeSettings;
            this.blockStoreCache = blockStoreCache;

            this.blocksToAnnounce = new AsyncQueue <ChainedBlock>();
            this.dequeueLoopTask  = this.DequeueContinuouslyAsync();
        }
Exemple #32
0
        public async Task DequeueAsync_GivenMultipleDequeues_ShouldCompleteInOrderedSequence()
        {
            //Arrange
            const int value1 = 1, value2 = 2;
            var       queue    = new AsyncQueue <int>();
            var       dequeue1 = queue.Dequeue();
            var       dequeue2 = queue.Dequeue();

            queue.Enqueue(value1);
            queue.Enqueue(value2);

            //Act
            var actual1 = await dequeue1;
            var actual2 = await dequeue2;

            //Assert
            Assert.Equal(value1, actual1);
            Assert.Equal(value2, actual2);

            //Annihilate
            queue.Dispose();
        }
Exemple #33
0
        public Day15()
        {
            AsyncQueue <long> programInputs  = new AsyncQueue <long>();
            AsyncQueue <long> programOutputs = new AsyncQueue <long>();
            IntCodeAsync      intCode        = new IntCodeAsync(_program, programInputs.Dequeue, programOutputs.Enqueue);
            Task vm = Task.Run(intCode.RunAsync);

            Task <long> sendCommand(Direction d)
            {
                programInputs.Enqueue((long)d + 1);
                return(programOutputs.Dequeue());
            }

            async Task backtrackHelper(IntVec2 position)
            {
                foreach (Direction d in s_directions)
                {
                    IntVec2 candidatePosition = position + s_deltas[(int)d];
                    if (!_map.ContainsKey(candidatePosition))
                    {
                        Status status = (Status) await sendCommand(d);

                        if (status != Status.Wall)
                        {
                            _map.Add(candidatePosition, status);
                            if (status == Status.Oxygen)
                            {
                                _oxygen = candidatePosition;
                            }

                            await backtrackHelper(candidatePosition);
                            await sendCommand(s_opposites[(int)d]);
                        }
                    }
                }
            }

            Task.Run(() => backtrackHelper(IntVec2.Zero)).Wait();
        }
Exemple #34
0
        public async Task AsyncQueue_DisposeCancelsEnqueueAsync()
        {
            bool signal = false;

            var asyncQueue = new AsyncQueue <int>(async(item, cancellation) =>
            {
                // We set the signal and wait and if the wait is finished, we reset the signal, but that should not happen.
                signal = true;
                await Task.Delay(500, cancellation);
                signal = false;
            });

            // Enqueue an item, which should trigger the callback.
            asyncQueue.Enqueue(1);

            // Wait a bit and dispose the queue, which should trigger the cancellation.
            await Task.Delay(100);

            asyncQueue.Dispose();

            Assert.True(signal);
        }
Exemple #35
0
        public async Task DequeueAfterCompleteWithData()
        {
            var queue = new AsyncQueue <int>();

            queue.Enqueue(42);
            queue.Complete();
            await queue.WhenCompletedTask.ConfigureAwait(false);

            Assert.Equal(42, await queue.DequeueAsync().ConfigureAwait(false));

            var threw = false;

            try
            {
                await queue.DequeueAsync().ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                threw = true;
            }
            Assert.True(threw);
        }
Exemple #36
0
        async void RunTcpListener(int port)
        {
            TcpListener listener;

            try
            {
                listener = new TcpListener(IPAddress.Any, port);
                listener.Start();
            }
            catch
            {
                MessageBox.Show($"Unable to connect to port {port}");
                return;
            }
            while (true)
            {
                var neoSocket = new NeoSocket(await listener.AcceptTcpClientAsync());
                var queue     = new AsyncQueue <byte[]>();
                Reader(neoSocket, queue);
                Writer(neoSocket, queue);
            }
        }
        public async Task AsyncSendAndClose(int count)
        {
            var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            var asyncQueue   = new AsyncQueue <int>();

            var sendValue = 0;
            var sendTask  = new Func <Task>(async() =>
            {
                await Task.Yield();
                while (cancellation.IsCancellationRequested == false)
                {
                    if (asyncQueue.TryEnqueue(sendValue++) == false)
                    {
                        return;
                    }
                    await Task.Delay(10).ConfigureAwait(false);
                }
            })().IgnoreFaultOrCancellation().ConfigureAwait(false);

            await Task.Delay(count);

            asyncQueue.ClearAndClose();

            await sendTask;

            var value     = default(int);
            var actualSum = 0;

            while (asyncQueue.TryDequeue(out value))
            {
                actualSum++;
            }
            var expectedSum = Enumerable.Range(0, sendValue).Sum();

            Assert.NotEqual(expectedSum, actualSum);
            Assert.NotEqual(0, asyncQueue.Count);
            cancellation.Cancel();
        }
Exemple #38
0
        /// <summary>
        /// Initializes parts of the object that are common for both inbound and outbound peers.
        /// </summary>
        /// <param name="inbound"><c>true</c> for inbound peers, <c>false</c> for outbound peers.</param>
        /// <param name="peerEndPoint">IP address and port on the side of the peer.</param>
        /// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
        /// <param name="parameters">Various settings and requirements related to how the connections with peers are going to be established, or <c>null</c> to use default parameters.</param>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory for creating loggers.</param>
        /// <param name="selfEndpointTracker">Tracker for endpoints known to be self.</param>
        /// <param name="onDisconnected">Callback that is invoked when peer has finished disconnecting, or <c>null</c> when no notification after the disconnection is required.</param>
        private NetworkPeer(bool inbound,
                            IPEndPoint peerEndPoint,
                            Network network,
                            NetworkPeerConnectionParameters parameters,
                            IDateTimeProvider dateTimeProvider,
                            ILoggerFactory loggerFactory,
                            ISelfEndpointTracker selfEndpointTracker,
                            Action <INetworkPeer> onDisconnected          = null,
                            Action <IPEndPoint, Payload> onSendingMessage = null)
        {
            this.dateTimeProvider = dateTimeProvider;

            this.preferredTransactionOptions = parameters.PreferredTransactionOptions;
            this.SupportedTransactionOptions = parameters.PreferredTransactionOptions & ~TransactionOptions.All;

            this.State                = inbound ? NetworkPeerState.Connected : NetworkPeerState.Created;
            this.Inbound              = inbound;
            this.PeerEndPoint         = peerEndPoint;
            this.RemoteSocketEndpoint = this.PeerEndPoint;
            this.RemoteSocketAddress  = this.RemoteSocketEndpoint.Address;
            this.RemoteSocketPort     = this.RemoteSocketEndpoint.Port;

            this.Network             = network;
            this.Behaviors           = new List <INetworkPeerBehavior>();
            this.selfEndpointTracker = selfEndpointTracker;

            this.onDisconnectedAsyncContext = new AsyncLocal <DisconnectedExecutionAsyncContext>();

            this.ConnectionParameters = parameters ?? new NetworkPeerConnectionParameters();
            this.MyVersion            = this.ConnectionParameters.CreateVersion(this.selfEndpointTracker.MyExternalAddress, this.PeerEndPoint, network, this.dateTimeProvider.GetTimeOffset());

            this.MessageReceived  = new AsyncExecutionEvent <INetworkPeer, IncomingMessage>();
            this.StateChanged     = new AsyncExecutionEvent <INetworkPeer, NetworkPeerState>();
            this.onDisconnected   = onDisconnected;
            this.onSendingMessage = onSendingMessage;

            this.asyncQueue = new AsyncQueue <Payload>(this.SendMessageHandledAsync);
        }
Exemple #39
0
        public static async Task RunAsyncQueueAsync(int count)
        {
            var cancellationToken = CancellationToken.None;

            var target = new AsyncQueue <string>(count, count);

            var PostTasks    = new Task[count];
            var dequeueTasks = new Task <string> [count];

            var PostSetupTask = Task.Run(() =>
            {
                for (int i = 0; i < count; i++)
                {
                    var item     = "HVQFSW613ACT65DOS2ILBA4G4QB3UP3K4PPZPB7UZ771SX9TX7DVNZR82W1TSHWDBZHIE8V6CGILADRFQ3QA76BOYA4T3XS7A8OQ3I2FCT8X04L2GXR3RY23WB2A0ZLNT58WCMZTY54PRPOVVENCMOJMCZC6D85H9HPGJ58BBOHN7PJ0G3QTDSB8K4ACT26QXG5D30WI";
                    PostTasks[i] = Task.Run(() => target.Post(item));
                }
            });

            var dequeueSetupTask = Task.Run(() =>
            {
                for (int i = 0; i < count; i++)
                {
                    dequeueTasks[i] = target.ReceiveAsync(cancellationToken);
                }
            });

            await Task.WhenAll(PostSetupTask, dequeueSetupTask);

            await Task.WhenAll(dequeueTasks);

            await Task.WhenAll(PostTasks);

            if (target.BufferCount != 0 ||
                target.PromisesCount != 0)
            {
                System.Console.WriteLine("AsyncQueue - Not empty");
            }
        }
Exemple #40
0
        public async Task WriteNotifications(Func <string, bool> shouldWriteByDb)
        {
            var receiveBuffer = new ArraySegment <byte>(new byte[1024]);
            var receive       = _webSocket.ReceiveAsync(receiveBuffer, _resourceShutdown);

            var asyncQueue = new AsyncQueue <DynamicJsonValue>();

            try
            {
                using (_notificationsBase.TrackActions(asyncQueue, this, shouldWriteByDb))
                {
                    AfterTrackActionsRegistration?.Invoke();

                    while (_resourceShutdown.IsCancellationRequested == false)
                    {
                        // we use this to detect client-initialized closure
                        if (receive.IsCompleted)
                        {
                            break;
                        }

                        var tuple = await asyncQueue.TryDequeueAsync(TimeSpan.FromSeconds(5));

                        if (tuple.Item1 == false)
                        {
                            await SendHeartbeat();

                            continue;
                        }

                        await WriteToWebSocket(tuple.Item2);
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }
        }
Exemple #41
0
        public HttpNegotiationQueue(WebSocketFactoryCollection standards, WebSocketConnectionExtensionCollection extensions, WebSocketListenerOptions options)
        {
            if (standards == null)
            {
                throw new ArgumentNullException(nameof(standards));
            }
            if (extensions == null)
            {
                throw new ArgumentNullException(nameof(extensions));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            this.log = options.Logger;

            _options    = options;
            _extensions = extensions;
            _cancel     = new CancellationTokenSource();
            _semaphore  = new SemaphoreSlim(options.ParallelNegotiations);

            _connections  = new AsyncQueue <NetworkConnection>(options.NegotiationQueueCapacity);
            _negotiations = new AsyncQueue <WebSocketNegotiationResult>();
            _negotiations.ParallelTakeErrorMessage = $"Parallel call to '{nameof(WebSocketListener.AcceptWebSocketAsync)}' is not allowed.";
            _negotiations.ClosedErrorMessage       = $"{nameof(WebSocketListener)} is closed and will not accept new connections.";

            //_cancel.Token.Register(() => this._connections.Close(new OperationCanceledException()));

            _handShaker = new WebSocketHandshaker(standards, _options);

            if (options.PingMode != PingMode.Manual)
            {
                this.pingQueue = new PingQueue(options.PingInterval);
            }

            WorkAsync().LogFault(this.log);
        }
Exemple #42
0
        static void Main(string[] args)
        {
            var queue  = AsyncQueue.Create <string>();
            var source = new CancellationTokenSource();
            var token  = source.Token;

            queue.Enqueue("1").Wait();
            queue.Enqueue("2").Wait();
            queue.Enqueue("3").Wait();
            queue.Enqueue("4").Wait();
            queue.Enqueue("5").Wait();
            queue.Enqueue("6").Wait();
            queue.Enqueue("7").Wait();
            queue.MaxCapacity  = 2;
            queue.OverflowRule = OverflowRule.DiscardFirst;
            queue.Enqueue("8").Wait();



            var senders   = Enumerable.Range(0, 3).Select(index => new Sender(index, queue, (ConsoleColor)(index + 13))).ToArray();
            var receivers = Enumerable.Range(0, 10).Select(index => new Receiver(index, queue, (ConsoleColor)(index + 5))).ToArray();

            Parallel.ForEach(receivers, async x => await x.Receive(token));

            Thread.Sleep(1000);
            var message = 0;

            Parallel.ForEach(senders, async x =>
            {
                for (var i = 0; i < 2000; i++)
                {
                    await x.Send(Interlocked.Increment(ref message).ToString());
                }
            });
            Console.ReadLine();
            source.Cancel();
            Console.ReadLine();
        }
Exemple #43
0
        public void Should_send_postponed_notification_when_postpone_date_reached()
        {
            using (var database = CreateDocumentDatabase())
            {
                database.NotificationCenter.Options.DatabaseStatsThrottle = TimeSpan.MaxValue;

                var alert1 = GetSampleAlert(customKey: "alert-1");
                database.NotificationCenter.Add(alert1);

                var alert2 = GetSampleAlert(customKey: "alert-2");
                database.NotificationCenter.Add(alert2);

                var alert3 = GetSampleAlert(customKey: "alert-3");
                database.NotificationCenter.Add(alert3);

                var actions = new AsyncQueue <DynamicJsonValue>();
                var writer  = new TestWebSocketWriter();

                using (database.NotificationCenter.TrackActions(actions, writer))
                {
                    database.NotificationCenter.Postpone(alert1.Id, SystemTime.UtcNow.AddDays(1));
                    database.NotificationCenter.Postpone(alert2.Id, SystemTime.UtcNow.AddMilliseconds(100));
                    database.NotificationCenter.Postpone(alert3.Id, SystemTime.UtcNow.AddDays(1));

                    for (int i = 0; i < 2; i++)
                    {
                        var posponed = actions.DequeueAsync().Result;

                        Assert.NotNull(posponed);
                        Assert.Equal(NotificationUpdateType.Postponed, posponed[(nameof(NotificationUpdated.UpdateType))]);
                    }

                    Assert.True(SpinWait.SpinUntil(() => writer.SentNotifications.Count == 1, TimeSpan.FromSeconds(30)), $"Got: {writer.SentNotifications.Count}");

                    Assert.Equal(alert2.Id, writer.SentNotifications[0]);
                }
            }
        }
Exemple #44
0
        public async Task DequeueAsync_GivenMultipleDequeues_ShouldCompleteInOrderedSequence()
        {
            //Arrange
            const int expected1 = 1, expected2 = 2;
            var       queue    = new AsyncQueue <int>();
            var       dequeue1 = queue.Dequeue();
            var       dequeue2 = queue.Dequeue();

            queue.Enqueue(expected1);
            queue.Enqueue(expected2);

            //Act
            var actual1 = await dequeue1.ConfigureAwait(false);

            var actual2 = await dequeue2.ConfigureAwait(false);

            //Assert
            actual1.Should().Be(expected1);
            actual2.Should().Be(expected2);

            //Annihilate
            queue.Dispose();
        }
Exemple #45
0
        public void TestEarlyCancellation()
        {
            var source =
                @"
namespace N1
{
    partial class Class
    {
        private void NonPartialMethod1() { }
        partial void PartialMethod();
    }
} 
";
            var tree        = CSharpSyntaxTree.ParseText(source, path: "file1");
            var eventQueue  = new AsyncQueue <CompilationEvent>();
            var compilation = CreateCompilationWithMscorlib45(new[] { tree })
                              .WithEventQueue(eventQueue);

            eventQueue.TryComplete(); // complete the queue before the compiler is finished with it
            var model = compilation.GetSemanticModel(tree);

            model.GetDiagnostics(tree.GetRoot().FullSpan);
        }
Exemple #46
0
        public void Can_postpone_notification_forever_then_next_notifictions_wont_be_sent()
        {
            using (var database = CreateDocumentDatabase())
            {
                database.NotificationCenter.Options.DatabaseStatsThrottle = TimeSpan.MaxValue;

                var alert = GetSampleAlert();

                database.NotificationCenter.Add(alert);

                var notifications = new AsyncQueue <DynamicJsonValue>();
                var writer        = new TestWebSocketWriter();

                database.NotificationCenter.Postpone(alert.Id, DateTime.MaxValue);

                using (database.NotificationCenter.TrackActions(notifications, writer))
                {
                    database.NotificationCenter.Add(alert);

                    Assert.Equal(0, notifications.Count);
                }
            }
        }
Exemple #47
0
        private async Task NewConnectionsHandler(AsyncQueue <OwnTcpSendMessage> processQueue)
        {
            try
            {
                while (IsOpen)
                {
                    TcpClient client = await listener.AcceptTcpClientAsync().ConfigureAwait(false);

                    OwnTcpSendQueue        queue      = new OwnTcpSendQueue();
                    OwnTcpServerConnection connection = new OwnTcpServerConnection(client, queue);

                    connections.Add(connection);

                    Task sendTask = Task.Run(() => SendClientHandler(connection));
                    Task readTask = Task.Run(() => ReadClientHandler(connection, processQueue));
                    connection.Task = Task.WhenAll(sendTask, readTask);
                }
            }
            catch (Exception e)
            {
                await CloseAsync(e, false);
            }
        }
Exemple #48
0
        static void Main(string[] args)
        {
            finishedTokenQ = new AsyncQueue <int>();

            int    receivePort        = 1001;
            int    sendPort           = 1000;
            string clientInstanceName = "client2";
            string serverInstanceName = "server1";

            Client2 client = new Client2(serverInstanceName);

            using (var c = AmbrosiaFactory.Deploy <IClient2.IClient2>(clientInstanceName, client, receivePort, sendPort))
            {
                while (finishedTokenQ.IsEmpty)
                {
                    Console.Write("Enter a message (hit ENTER to send): ");
                    string input = Console.ReadLine();
                    client.IngressKeyboardInput(input);
                    Thread.Sleep(1000);
                }
                finishedTokenQ.DequeueAsync().Wait();
            }
        }
Exemple #49
0
        public async Task Enumerate()
        {
            var queue = new AsyncQueue <int>();
            var list  = new List <int>();

            var task = Task.Run(async() =>
            {
                await foreach (var i in queue)
                {
                    list.Add(i);
                }
            });

            for (var i = 0; i < 100; i++)
            {
                Assert.That(queue.TryWrite(i), Is.True);
            }

            queue.Complete();
            await task;

            Assert.That(list.Count, Is.EqualTo(100));
        }
Exemple #50
0
        public static async Task RunAsyncQueueAsync(int count)
        {
            var cancellationToken = CancellationToken.None;

            var target = new AsyncQueue<string>(count, count);

            var PostTasks = new Task[count];
            var dequeueTasks = new Task<string>[count];

            var PostSetupTask = Task.Run(() =>
            {
                for (int i = 0; i < count; i++)
                {
                    var item = "HVQFSW613ACT65DOS2ILBA4G4QB3UP3K4PPZPB7UZ771SX9TX7DVNZR82W1TSHWDBZHIE8V6CGILADRFQ3QA76BOYA4T3XS7A8OQ3I2FCT8X04L2GXR3RY23WB2A0ZLNT58WCMZTY54PRPOVVENCMOJMCZC6D85H9HPGJ58BBOHN7PJ0G3QTDSB8K4ACT26QXG5D30WI";
                    PostTasks[i] = Task.Run(() => target.Post(item));
                }
            });

            var dequeueSetupTask = Task.Run(() =>
            {
                for (int i = 0; i < count; i++)
                {
                    dequeueTasks[i] = target.ReceiveAsync(cancellationToken);
                }
            });

            await Task.WhenAll(PostSetupTask, dequeueSetupTask);
            await Task.WhenAll(dequeueTasks);
            await Task.WhenAll(PostTasks);

            if (target.BufferCount != 0 ||
                target.PromisesCount != 0)
            {
                System.Console.WriteLine("AsyncQueue - Not empty");
            }
        }
Exemple #51
0
        public async static void Run(string slidesQuery, string size, Action <string> action, CancellationToken token)
        {
            if (string.IsNullOrWhiteSpace(slidesQuery))
            {
                return;
            }

            var inputQueries = new AsyncQueue <string>();

            slidesQuery.Split('\n').ToList().ForEach(query => inputQueries.Enqueue(query));
            inputQueries.SetFinished();
            var outputURLs = new AsyncQueue <List <string> >();

            AsyncHelper.RunTasks(inputQueries, query => GetSlideURLs(query, size, token), outputURLs, token);

            var urls = new List <string>();

            while (await outputURLs.HasItemsAsync(token))
            {
                foreach (var item in outputURLs.Dequeue())
                {
                    urls.Add(item);
                }
            }

            var random = new Random();

            urls = urls.OrderBy(x => random.Next()).ToList();

            var inputURLs = new AsyncQueue <string>();

            urls.ForEach(url => inputURLs.Enqueue(url));
            inputURLs.SetFinished();

            SlideDownloader.Run(inputURLs, action, token);
        }
Exemple #52
0
        private PhpCompilation(
            string assemblyName,
            PhpCompilationOptions options,
            ImmutableArray <MetadataReference> references,
            bool isSubmission,
            ReferenceManager referenceManager = null,
            bool reuseReferenceManager        = false,
            //SyntaxAndDeclarationManager syntaxAndDeclarations
            AsyncQueue <CompilationEvent> eventQueue = null
            )
            : base(assemblyName, references, SyntaxTreeCommonFeatures(ImmutableArray <SyntaxTree> .Empty), isSubmission, eventQueue)
        {
            _wellKnownMemberSignatureComparer = new WellKnownMembersSignatureComparer(this);

            _options              = options;
            _tables               = new SourceSymbolCollection(this);
            _coreTypes            = new CoreTypes(this);
            _coreMethods          = new CoreMethods(_coreTypes);
            _anonymousTypeManager = new AnonymousTypeManager(this);

            _referenceManager = (reuseReferenceManager && referenceManager != null)
                ? referenceManager
                : new ReferenceManager(MakeSourceAssemblySimpleName(), options.AssemblyIdentityComparer, referenceManager?.ObservedMetadata, options.SdkDirectory);
        }
Exemple #53
0
        public BlockStoreQueue(
            IBlockRepository blockRepository,
            ConcurrentChain chain,
            IChainState chainState,
            StoreSettings storeSettings,
            INodeLifetime nodeLifetime,
            ILoggerFactory loggerFactory)
        {
            Guard.NotNull(blockRepository, nameof(blockRepository));
            Guard.NotNull(chain, nameof(chain));
            Guard.NotNull(chainState, nameof(chainState));
            Guard.NotNull(storeSettings, nameof(storeSettings));
            Guard.NotNull(nodeLifetime, nameof(nodeLifetime));
            Guard.NotNull(loggerFactory, nameof(loggerFactory));

            this.chainState      = chainState;
            this.nodeLifetime    = nodeLifetime;
            this.storeSettings   = storeSettings;
            this.chain           = chain;
            this.blockRepository = blockRepository;

            this.blocksQueue = new AsyncQueue <BlockPair>();
            this.logger      = loggerFactory.CreateLogger(this.GetType().FullName);
        }
        public async Task ParallelSendAndCloseReceiveAll(int count)
        {
            var cancellationSource = new CancellationTokenSource();
            var asyncQueue         = new AsyncQueue <int>();
            var options            = new ParallelOptions {
                CancellationToken = cancellationSource.Token, MaxDegreeOfParallelism = Environment.ProcessorCount / 2, TaskScheduler = TaskScheduler.Default
            };
            var items = new ConcurrentQueue <int>(Enumerable.Range(0, count));

            var sendTask = Task.Factory.StartNew(() => Parallel.For(0, count, options, i =>
            {
                var item = default(int);
                if (items.TryDequeue(out item))
                {
                    if (asyncQueue.TryEnqueue(item) == false)
                    {
                        items.Enqueue(item);
                    }
                }
            }));

            await Task.Delay(1).ConfigureAwait(false);

            var itemsInAsyncQueue = asyncQueue.TakeAllAndClose(); // deny TryEnqueue

            cancellationSource.Cancel();                          // stop parallel for

            await sendTask.IgnoreFaultOrCancellation().ConfigureAwait(false);

            var actualCount = items.Count + itemsInAsyncQueue.Count;

            this.logger.Debug($"[TEST] en-queued: {itemsInAsyncQueue.Count}, total: {count}");

            Assert.Equal(count, actualCount);
            Assert.Equal(0, asyncQueue.Count);
        }
Exemple #55
0
        static void Main(string[] args)
        {
            finishedTokenQ = new AsyncQueue <int>();

            int    receivePort        = 1001;
            int    sendPort           = 1000;
            string clientInstanceName = "client";
            string serverInstanceName = "server";

            if (args.Length >= 1)
            {
                clientInstanceName = args[0];
            }

            if (args.Length == 2)
            {
                serverInstanceName = args[1];
            }

            using (var c = AmbrosiaFactory.Deploy <IClient3>(clientInstanceName, new Client3(serverInstanceName), receivePort, sendPort))
            {
                finishedTokenQ.DequeueAsync().Wait();
            }
        }
Exemple #56
0
        public async Task Duplicated_notification_should_not_arrive_before_postponed_until_date()
        {
            using (var database = CreateDocumentDatabase())
            {
                database.NotificationCenter.Options.DatabaseStatsThrottle = TimeSpan.MaxValue;

                var alert = GetSampleAlert();
                database.NotificationCenter.Add(alert);

                database.NotificationCenter.Postpone(alert.Id, SystemTime.UtcNow.AddDays(1));

                var actions = new AsyncQueue <DynamicJsonValue>();
                var writer  = new TestWebSocketWriter();

                using (database.NotificationCenter.TrackActions(actions, writer))
                {
                    database.NotificationCenter.Add(alert);
                    Assert.False((await actions.TryDequeueAsync(TimeSpan.FromMilliseconds(10))).Item1);

                    database.NotificationCenter.Add(alert);
                    Assert.False((await actions.TryDequeueAsync(TimeSpan.FromMilliseconds(10))).Item1);
                }
            }
        }
 /// <summary>
 /// Returns a new compilation with a given event queue.
 /// </summary>
 public override Compilation WithEventQueue(AsyncQueue<CompilationEvent> eventQueue)
 {
     return new CSharpCompilation(
         this.AssemblyName,
         this.options,
         this.ExternalReferences,
         this.SyntaxTrees,
         this.syntaxTreeOrdinalMap,
         this.rootNamespaces,
         this.declarationTable,
         this.previousSubmission,
         this.SubmissionReturnType,
         this.HostObjectType,
         this.IsSubmission,
         this.referenceManager,
         reuseReferenceManager: true,
         eventQueue: eventQueue);
 }
        private CSharpCompilation(
            string assemblyName,
            CSharpCompilationOptions options,
            ImmutableArray<MetadataReference> references,
            ImmutableArray<SyntaxTree> syntaxTrees,
            ImmutableDictionary<SyntaxTree, int> syntaxTreeOrdinalMap,
            ImmutableDictionary<SyntaxTree, Lazy<RootSingleNamespaceDeclaration>> rootNamespaces,
            DeclarationTable declarationTable,
            CSharpCompilation previousSubmission,
            Type submissionReturnType,
            Type hostObjectType,
            bool isSubmission,
            ReferenceManager referenceManager,
            bool reuseReferenceManager,
            AsyncQueue<CompilationEvent> eventQueue = null)
            : base(assemblyName, references, submissionReturnType, hostObjectType, isSubmission, syntaxTreeOrdinalMap, eventQueue)
        {
            using (Logger.LogBlock(FunctionId.CSharp_Compilation_Create, message: assemblyName))
            {
                this.wellKnownMemberSignatureComparer = new WellKnownMembersSignatureComparer(this);
                this.options = options;
                this.syntaxTrees = syntaxTrees;

                this.rootNamespaces = rootNamespaces;
                this.declarationTable = declarationTable;

                Debug.Assert(syntaxTrees.All(tree => syntaxTrees[syntaxTreeOrdinalMap[tree]] == tree));
                Debug.Assert(syntaxTrees.SetEquals(rootNamespaces.Keys.AsImmutable(), EqualityComparer<SyntaxTree>.Default));

                this.builtInOperators = new BuiltInOperators(this);
                this.scriptClass = new Lazy<ImplicitNamedTypeSymbol>(BindScriptClass);
                this.globalImports = new Lazy<Imports>(BindGlobalUsings);
                this.globalNamespaceAlias = new Lazy<AliasSymbol>(CreateGlobalNamespaceAlias);
                this.anonymousTypeManager = new AnonymousTypeManager(this);

                if (isSubmission)
                {
                    Debug.Assert(previousSubmission == null || previousSubmission.HostObjectType == hostObjectType);

                    this.previousSubmission = previousSubmission;
                }
                else
                {
                    Debug.Assert(previousSubmission == null && submissionReturnType == null && hostObjectType == null);
                }

                if (reuseReferenceManager)
                {
                    referenceManager.AssertCanReuseForCompilation(this);
                    this.referenceManager = referenceManager;
                }
                else
                {
                    this.referenceManager = new ReferenceManager(
                        MakeSourceAssemblySimpleName(),
                        options.AssemblyIdentityComparer,
                        (referenceManager != null) ? referenceManager.ObservedMetadata : null);
                }

                Debug.Assert((object)this.lazyAssemblySymbol == null);
                if (EventQueue != null) EventQueue.Enqueue(new CompilationStartedEvent(this));
            }
        }
        private static bool DequeueCompilationEvents(AsyncQueue<CompilationEvent> eventQueue, out bool compilationStartedFired, out HashSet<string> declaredSymbolNames, out HashSet<string> completedCompilationUnits)
        {
            compilationStartedFired = false;
            declaredSymbolNames = new HashSet<string>();
            completedCompilationUnits = new HashSet<string>();
            if (eventQueue.Count == 0)
            {
                return false;
            }

            CompilationEvent compEvent;
            while (eventQueue.TryDequeue(out compEvent))
            {
                if (compEvent is CompilationStartedEvent)
                {
                    Assert.False(compilationStartedFired, "Unexpected multiple compilation stated events");
                    compilationStartedFired = true;
                }
                else
                {
                    var symbolDeclaredEvent = compEvent as SymbolDeclaredCompilationEvent;
                    if (symbolDeclaredEvent != null)
                    {
                        var added = declaredSymbolNames.Add(symbolDeclaredEvent.Symbol.Name);
                        Assert.True(added, "Unexpected multiple symbol declared events for symbol " + symbolDeclaredEvent.Symbol);
                    }
                    else
                    {
                        var compilationCompeletedEvent = compEvent as CompilationUnitCompletedEvent;
                        if (compilationCompeletedEvent != null)
                        {
                            Assert.True(completedCompilationUnits.Add(compilationCompeletedEvent.CompilationUnit.FilePath));
                        }
                    }
                }
            }

            return true;
        }
        public void TestCompilationEventsForPartialMethod()
        {
            var source1 = @"
namespace N1
{
    partial class Class
    {
        private void NonPartialMethod1() { }
        partial void PartialMethod();
    }
} 
";
            var source2 = @"
namespace N1
{
    partial class Class
    {
        private void NonPartialMethod2() { }
        partial void PartialMethod() { }
    }
} 
";

            var tree1 = CSharpSyntaxTree.ParseText(source1, path: "file1");
            var tree2 = CSharpSyntaxTree.ParseText(source2, path: "file2");
            var eventQueue = new AsyncQueue<CompilationEvent>();
            var compilation = CreateCompilationWithMscorlib45(new[] { tree1, tree2 }).WithEventQueue(eventQueue);

            // Invoke SemanticModel.GetDiagnostics to force populate the event queue for symbols in the first source file.
            var model = compilation.GetSemanticModel(tree1);
            model.GetDiagnostics(tree1.GetRoot().FullSpan);

            Assert.True(eventQueue.Count > 0);
            bool compilationStartedFired;
            HashSet<string> declaredSymbolNames, completedCompilationUnits;
            Assert.True(DequeueCompilationEvents(eventQueue, out compilationStartedFired, out declaredSymbolNames, out completedCompilationUnits));

            // Verify symbol declared events fired for all symbols declared in the first source file.
            Assert.True(compilationStartedFired);
            Assert.True(declaredSymbolNames.Contains(compilation.GlobalNamespace.Name));
            Assert.True(declaredSymbolNames.Contains("N1"));
            Assert.True(declaredSymbolNames.Contains("Class"));
            Assert.True(declaredSymbolNames.Contains("NonPartialMethod1"));
            Assert.True(declaredSymbolNames.Contains("PartialMethod"));
            Assert.True(completedCompilationUnits.Contains(tree1.FilePath));
        }