Exemplo n.º 1
0
        private async Task process()
        {
            InitializeQueue();

            var response = await _sub.PullAsync(_subscriptionName, false, 1,
                                                CallSettings.FromCallTiming(
                                                    CallTiming.FromExpiration(
                                                        Expiration.FromTimeout(
                                                            TimeSpan.FromSeconds(90)))));

            if (response.ReceivedMessages == null || response.ReceivedMessages.Count == 0)
            {
                return;
            }

            var message   = response.ReceivedMessages[0];
            var jsonBytes = message.Message.Data.ToByteArray();
            var payload   = Encoding.UTF8.GetString(jsonBytes);

            _appLogger.LogMessage($"Message Type: {typeof(T).FullName} dequeued with MessageId: {message.Message.MessageId}");

            _messageHandler(payload);

            await _sub.AcknowledgeAsync(_subscriptionName, new string[] { message.AckId });
        }
Exemplo n.º 2
0
        public void FromCallTiming()
        {
            Assert.Null(CallSettings.FromCallTiming(null));
            var timing   = CallTiming.FromExpiration(Expiration.None);
            var settings = CallSettings.FromCallTiming(timing);

            Assert.Same(timing, settings.Timing);
        }
        public void SucceedWithExpiration()
        {
            var apiCall = ApiServerStreamingCall.Create <int, int>(
                (request, callOptions) => null,
                CallSettings.FromCallTiming(CallTiming.FromExpiration(Expiration.FromTimeout(TimeSpan.FromSeconds(100)))),
                new FakeClock());

            Assert.Null(apiCall.Call(0, null));
        }
Exemplo n.º 4
0
        public void WithEarlierDeadline_DeadlineIsLaterThanExistingDeadline()
        {
            // Use a cancellation token to emphasize that it's not just the timing.
            var          token    = new CancellationTokenSource().Token;
            CallSettings settings = CallSettings.FromCancellationToken(token)
                                    .WithCallTiming(CallTiming.FromExpiration(Expiration.FromDeadline(new DateTime(10L, DateTimeKind.Utc))));
            DateTime?deadline = new DateTime(20L, DateTimeKind.Utc);

            Assert.Same(settings, settings.WithEarlierDeadline(deadline, new FakeClock()));
        }
Exemplo n.º 5
0
        public void SucceedWithExpiration()
        {
            var apiCall = ApiBidirectionalStreamingCall.Create <int, int>(
                callOptions => null,
                CallSettings.FromCallTiming(CallTiming.FromExpiration(Expiration.FromTimeout(TimeSpan.FromSeconds(100)))),
                new BidirectionalStreamingSettings(100),
                new FakeClock());

            Assert.Null(apiCall.Call(null));
        }
Exemplo n.º 6
0
        public void WithCallTiming_NullSettings()
        {
            CallSettings noSettings = null;

            Assert.Null(noSettings.WithCallTiming(null));
            CallTiming timing = CallTiming.FromExpiration(Expiration.None);
            var        result = noSettings.WithCallTiming(timing);

            Assert.Same(timing, result.Timing);
        }
Exemplo n.º 7
0
        public void ToCallOptions_CallTimingExpirationTimeout()
        {
            var          clock        = new FakeClock();
            var          timeout      = TimeSpan.FromSeconds(1);
            CallSettings callSettings = CallSettings.FromCallTiming(CallTiming.FromExpiration(Expiration.FromTimeout(timeout)));
            var          options      = callSettings.ToCallOptions(clock);

            // Value should be exact, as we control time precisely.
            Assert.Equal(options.Deadline.Value, clock.GetCurrentDateTimeUtc() + timeout);
        }
Exemplo n.º 8
0
        public void ToCallOptions_CallTimingExpirationDeadline()
        {
            var          deadline     = new DateTime(2015, 6, 19, 5, 2, 3, DateTimeKind.Utc);
            var          mockClock    = new Mock <IClock>();
            CallSettings callSettings = CallSettings.FromCallTiming(CallTiming.FromExpiration(Expiration.FromDeadline(deadline)));
            var          options      = callSettings.ToCallOptions(mockClock.Object);

            // Value should be exact, as we control time precisely.
            Assert.Equal(options.Deadline.Value, deadline);
            mockClock.Verify(c => c.GetCurrentDateTimeUtc(), Times.Never);
        }
Exemplo n.º 9
0
        public void WithEarlierDeadline_DeadlineIsLaterThanExistingTimeout()
        {
            // Use a cancellation token to emphasize that it's not just the timing.
            var          token    = new CancellationTokenSource().Token;
            CallSettings settings = CallSettings.FromCancellationToken(token)
                                    .WithCallTiming(CallTiming.FromExpiration(Expiration.FromTimeout(TimeSpan.FromTicks(100))));
            var      clock    = new FakeClock();
            DateTime?deadline = clock.GetCurrentDateTimeUtc() + TimeSpan.FromTicks(200);

            Assert.Same(settings, settings.WithEarlierDeadline(deadline, clock));
        }
Exemplo n.º 10
0
        public void WithCallTiming_NullTiming()
        {
            CallTiming        timing = CallTiming.FromExpiration(Expiration.None);
            CancellationToken token  = new CancellationTokenSource().Token;

            var original = new CallSettings(token, null, timing, null, null, null);
            var result   = original.WithCallTiming(null);

            Assert.Null(result.Timing);
            Assert.Equal(token, result.CancellationToken);
        }
Exemplo n.º 11
0
        public void WithCancellationToken()
        {
            CancellationToken token1 = new CancellationTokenSource().Token;
            CallTiming        timing = CallTiming.FromExpiration(Expiration.None);
            var original             = new CallSettings(token1, null, timing, null, null, null);

            CancellationToken token2 = new CancellationTokenSource().Token;
            var result = original.WithCancellationToken(token2);

            Assert.Same(timing, result.Timing);
            Assert.Equal(token2, result.CancellationToken);
        }
Exemplo n.º 12
0
        public void WithCallTiming_NonNull()
        {
            CallTiming timing1 = CallTiming.FromExpiration(Expiration.None);
            CallTiming timing2 = CallTiming.FromDeadline(DateTime.UtcNow);

            CancellationToken token = new CancellationTokenSource().Token;

            var original = new CallSettings(token, null, timing1, null, null, null);
            var result   = original.WithCallTiming(timing2);

            Assert.Same(timing2, result.Timing);
            Assert.Equal(token, result.CancellationToken);
        }
Exemplo n.º 13
0
        public void MergedWith_BothNonNull()
        {
            CallTiming timing1 = CallTiming.FromExpiration(Expiration.None);
            CallTiming timing2 = CallTiming.FromDeadline(DateTime.UtcNow);

            CancellationToken token = new CancellationTokenSource().Token;

            var settings1 = new CallSettings(token, null, timing1, null, null, null);
            var settings2 = new CallSettings(null, null, timing2, null, null, null);
            var merged    = settings1.MergedWith(settings2);

            Assert.Equal(token, merged.CancellationToken);
            Assert.Same(timing2, merged.Timing);
        }
Exemplo n.º 14
0
        public void WithEarlierDeadline_DeadlineIsEarlierThanExistingTimeout()
        {
            // Use a cancellation token to emphasize that it's not just the timing.
            var          token    = new CancellationTokenSource().Token;
            CallSettings settings = CallSettings.FromCancellationToken(token)
                                    .WithCallTiming(CallTiming.FromExpiration(Expiration.FromTimeout(TimeSpan.FromTicks(200))));
            var          clock       = new FakeClock();
            DateTime?    deadline    = clock.GetCurrentDateTimeUtc() + TimeSpan.FromTicks(100);
            CallSettings newSettings = settings.WithEarlierDeadline(deadline, new FakeClock());

            // New timing has a deadline rather than a timeout
            Assert.Equal(deadline, newSettings.Timing.Expiration.Deadline);
            Assert.Equal(token, newSettings.CancellationToken);
        }
        /// <summary>
        /// Makes one call to PubSub.Pull to pull some books from the subscription.
        /// </summary>
        // [START pullonce]
        private void PullOnce(Action <long> callback, CancellationToken cancellationToken)
        {
            _logger.LogVerbose($"Pulling messages from {_subscriptionName}...");
            // Pull some messages from the subscription.

            var response = _sub.Pull(_subscriptionName, false, 3,
                                     CallSettings.FromCallTiming(
                                         CallTiming.FromExpiration(
                                             Expiration.FromTimeout(
                                                 TimeSpan.FromSeconds(90)))));

            if (response.ReceivedMessages == null)
            {
                // HTTP Request expired because the queue was empty.  Ok.
                _logger.LogVerbose("Pulled no messages.");
                return;
            }
            _logger.LogVerbose($"Pulled {response.ReceivedMessages.Count} messages.");
            if (response.ReceivedMessages.Count == 0)
            {
                return;
            }
            foreach (var message in response.ReceivedMessages)
            {
                try
                {
                    // Unpack the message.
                    byte[] json     = message.Message.Data.ToByteArray();
                    var    qmessage = JsonConvert.DeserializeObject <QueueMessage>(
                        Encoding.UTF8.GetString(json));
                    // Invoke ProcessBook().
                    callback(qmessage.BookId);
                }
                catch (Exception e)
                {
                    _logger.LogError("Error processing book.", e);
                }
            }
            // Acknowledge the message so we don't see it again.
            var ackIds = new string[response.ReceivedMessages.Count];

            for (int i = 0; i < response.ReceivedMessages.Count; ++i)
            {
                ackIds[i] = response.ReceivedMessages[i].AckId;
            }
            _sub.Acknowledge(_subscriptionName, ackIds);
        }
        public FirebaseDocumentListener(FirestoreDb db)
        {
            // Create client
            FirestoreDb     = db;
            FirestoreClient = db.Client; //FirestoreClient.Create();
            ProjectId       = db.ProjectId;
            DatabaseId      = db.DatabaseId;

            //Setup no expiration for the listen
            ListenSettings = CallSettings.FromCallTiming(CallTiming.FromExpiration(Expiration.None));

            //Start our handler for writing requests to GCP
            RequestHanderTask = StartRequestHandlerTask();

            //Initialize a cancelation source so we can cancel tasks we create
            CancellationTokenSource = new CancellationTokenSource();
            CancellationToken       = CancellationTokenSource.Token;
        }
Exemplo n.º 17
0
        public void ToCallOptions_All()
        {
            var mockClock    = new Mock <IClock>();
            var callSettings = new CallSettings
                               (
                headerMutation: metadata => metadata.Add(new Metadata.Entry("1", "one")),
                timing: CallTiming.FromExpiration(Expiration.None),
                cancellationToken: new CancellationTokenSource().Token,
                writeOptions: new WriteOptions(WriteFlags.NoCompress),
                propagationToken: null, // Not possible to create/mock
                credentials: null       // Not possible to create/mock
                               );
            var options = callSettings.ToCallOptions(mockClock.Object);

            Assert.Equal(1, options.Headers.Count);
            Assert.Equal("[Entry: key=1, value=one]", options.Headers[0].ToString());
            Assert.Null(options.Deadline);
            Assert.Equal(callSettings.CancellationToken, options.CancellationToken);
            Assert.Same(callSettings.WriteOptions, options.WriteOptions);
        }