Beispiel #1
0
        public void WithExpiration_NullExpiration()
        {
            var          token    = new CancellationTokenSource().Token;
            CallSettings settings = CallSettings.FromCancellationToken(token);

            Assert.Throws <ArgumentNullException>(() => settings.WithExpiration(null));
        }
Beispiel #2
0
        /// <summary>
        /// Updates the call settings with configuration parameters.
        /// </summary>
        /// <param name="callSettings">The call settings.</param>
        /// <param name="config">The configuration.</param>
        /// <param name="serviceContext">The service context.</param>
        /// <returns></returns>
        private CallSettings UpdateCallSettingsWithConfigParameters(CallSettings callSettings,
                                                                    GoogleAdsConfig config, GoogleAdsServiceContext serviceContext)
        {
            callSettings = callSettings.WithHeader(MetadataKeyNames.DeveloperToken,
                                                   config.DeveloperToken)
                           .WithResponseMetadataHandler(delegate(Metadata metadata)
            {
                AdsResponseMetadata responseMetadata = new AdsResponseMetadata(metadata);
                serviceContext.OnResponseMetadataReceived(responseMetadata);
            });

            if (!string.IsNullOrEmpty(config.LoginCustomerId))
            {
                callSettings = callSettings.WithHeader(MetadataKeyNames.LoginCustomerId,
                                                       config.LoginCustomerId);
            }

            if (!string.IsNullOrEmpty(config.LinkedCustomerId))
            {
                callSettings = callSettings.WithHeader(MetadataKeyNames.LinkedCustomerId,
                                                       config.LinkedCustomerId);
            }

            if (!string.IsNullOrEmpty(config.LibraryIdentifierOverride))
            {
                callSettings = callSettings.WithHeader(MetadataKeyNames.LibraryIdentifier,
                                                       config.LibraryIdentifierOverride);
            }

            callSettings = callSettings.WithExpiration(Expiration.FromTimeout(
                                                           TimeSpan.FromMilliseconds(config.Timeout)));

            return(callSettings);
        }
Beispiel #3
0
        public void WithExpiration_BothNull()
        {
            CallSettings settings   = null;
            Expiration   expiration = null;
            var          result     = settings.WithExpiration(expiration);

            Assert.Null(result);
        }
Beispiel #4
0
        public void WithExpiration_NullSettings()
        {
            CallSettings settings   = null;
            Expiration   expiration = Expiration.FromTimeout(TimeSpan.FromSeconds(1));
            var          result     = settings.WithExpiration(expiration);

            Assert.Equal(expiration, result.Expiration);
        }
Beispiel #5
0
        public void WithExpiration_NullExpiration()
        {
            var          token    = new CancellationTokenSource().Token;
            CallSettings settings = CallSettings.FromCancellationToken(token);
            var          result   = settings.WithExpiration(null);

            Assert.Equal(token, result.CancellationToken);
            Assert.Null(result.Expiration);
        }
Beispiel #6
0
        public void WithExpiration_SettingsWithExpiration()
        {
            var          token      = new CancellationTokenSource().Token;
            CallSettings settings   = CallSettings.FromCancellationToken(token).WithTimeout(TimeSpan.FromSeconds(5));
            Expiration   expiration = Expiration.FromTimeout(TimeSpan.FromSeconds(1));
            var          result     = settings.WithExpiration(expiration);

            Assert.Same(expiration, result.Expiration);
            Assert.Equal(token, result.CancellationToken);
        }
Beispiel #7
0
        public void WithExpiration_SettingsWithRetry()
        {
            var          token           = new CancellationTokenSource().Token;
            var          backoffSettings = new BackoffSettings(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(10), 1.5);
            var          retry           = new RetrySettings(backoffSettings, backoffSettings, Expiration.FromTimeout(TimeSpan.FromSeconds(5)));
            var          originalTiming  = CallTiming.FromRetry(retry);
            CallSettings settings        = CallSettings.FromCancellationToken(token).WithCallTiming(originalTiming);
            Expiration   expiration      = Expiration.FromTimeout(TimeSpan.FromSeconds(1));
            var          result          = settings.WithExpiration(expiration);

            Assert.Same(expiration, result.Timing.Retry.TotalExpiration);
            Assert.Same(backoffSettings, result.Timing.Retry.RetryBackoff);
            Assert.Same(backoffSettings, result.Timing.Retry.TimeoutBackoff);
            Assert.Equal(token, result.CancellationToken);
        }
Beispiel #8
0
        /// <summary>
        /// Asynchronously polls the operation until it is complete, returning the completed operation.
        /// </summary>
        /// <remarks>
        /// <para>
        /// If this object already represents a completed operation, it is returned with no further RPCs.
        /// If <paramref name="metadataCallback"/> is non-null, the callback will be called with any metadata
        /// present before the result is returned.
        /// </para>
        /// <para>
        /// No guarantee is made as to which thread is used for metadata callbacks. However, each callback is
        /// performed synchronously: this method waits for the callback to complete before the operation is next polled.
        /// This guarantees that for a single call, metadata callbacks will never occur in parallel.
        /// </para>
        /// </remarks>
        /// <param name="pollSettings">The settings to use for repeated polling, or null
        /// to use the default poll settings (poll once every 10 seconds, forever).</param>
        /// <param name="callSettings">The call settings to apply on each call, or null for default settings.</param>
        /// <param name="metadataCallback">The callback to invoke with the metadata from each poll operation, even if the metadata is null.</param>
        /// <returns>The completed operation, which can then be checked for errors or a result.</returns>
        public Task <Operation <TResponse, TMetadata> > PollUntilCompletedAsync(
            PollSettings pollSettings           = null,
            CallSettings callSettings           = null,
            Action <TMetadata> metadataCallback = null)
        {
            if (IsCompleted)
            {
                metadataCallback?.Invoke(Metadata);
                return(Task.FromResult(this));
            }

            // We need to work out the effective expiration so that we can truncate any deadline, but anything else
            // that's in the effective call settings can be left to the normal merging process. In particular,
            // we don't want to include the header mutation that adds the version header, as otherwise we'll end up
            // including it twice.
            var effectiveExpiration = Client.GetEffectiveCallSettingsForGetOperation(callSettings)?.Expiration;

            if (effectiveExpiration != null)
            {
                callSettings = callSettings.WithExpiration(effectiveExpiration);
            }
            // TODO: Retry settings?

            Func <DateTime?, Task <Operation <TResponse, TMetadata> > > pollAction =
                async deadline =>
            {
                var result = await PollOnceAsync(callSettings.WithEarlierDeadline(deadline, Client.Clock)).ConfigureAwait(false);

                metadataCallback?.Invoke(result.Metadata);
                return(result);
            };

            return(Polling.PollRepeatedlyAsync(
                       pollAction, o => o.IsCompleted,
                       Client.Clock, Client.Scheduler, pollSettings ?? Client.DefaultPollSettings ?? s_defaultPollSettings,
                       callSettings?.CancellationToken ?? CancellationToken.None));
        }
Beispiel #9
0
 private CallSettings CreateSettings(CallSettings originalSettings, int timeoutSeconds, CancellationToken cancellationToken) =>
 originalSettings
 .WithExpiration(Client.Settings.ConvertTimeoutToExpiration(timeoutSeconds))
 .WithCancellationToken(cancellationToken);