Пример #1
0
        /// <summary>
        /// Returns new instance of <see cref="CallOptions"/> with
        /// <c>PropagationToken</c> set to the value provided. Values of all other fields are preserved.
        /// </summary>
        /// <param name="propagationToken">The context propagation token.</param>
        public CallOptions WithPropagationToken(ContextPropagationToken propagationToken)
        {
            var newOptions = this;

            newOptions.propagationToken = propagationToken;
            return(newOptions);
        }
        public void WithMethods()
        {
            var options = new CallOptions();
            
            var metadata = new Metadata();
            Assert.AreSame(metadata, options.WithHeaders(metadata).Headers);

            var deadline = DateTime.UtcNow;
            Assert.AreEqual(deadline, options.WithDeadline(deadline).Deadline.Value);

            var cancellationToken = new CancellationTokenSource().Token;
            Assert.AreEqual(cancellationToken, options.WithCancellationToken(cancellationToken).CancellationToken);

            var writeOptions = new WriteOptions();
            Assert.AreSame(writeOptions, options.WithWriteOptions(writeOptions).WriteOptions);

            var propagationToken = new ContextPropagationToken(CallSafeHandle.NullInstance, DateTime.UtcNow, 
                CancellationToken.None, ContextPropagationOptions.Default);
            Assert.AreSame(propagationToken, options.WithPropagationToken(propagationToken).PropagationToken);

            var credentials = new FakeCallCredentials();
            Assert.AreSame(credentials, options.WithCredentials(credentials).Credentials);

            // Check that the original instance is unchanged.
            Assert.IsNull(options.Headers);
            Assert.IsNull(options.Deadline);
            Assert.AreEqual(CancellationToken.None, options.CancellationToken);
            Assert.IsNull(options.WriteOptions);
            Assert.IsNull(options.PropagationToken);
            Assert.IsNull(options.Credentials);
        }
Пример #3
0
 /// <summary>
 /// Creates a new instance of <c>CallOptions</c> struct.
 /// </summary>
 /// <param name="headers">Headers to be sent with the call.</param>
 /// <param name="deadline">Deadline for the call to finish. null means no deadline.</param>
 /// <param name="cancellationToken">Can be used to request cancellation of the call.</param>
 /// <param name="writeOptions">Write options that will be used for this call.</param>
 /// <param name="propagationToken">Context propagation token obtained from <see cref="ServerCallContext"/>.</param>
 public CallOptions(Metadata headers          = null, DateTime?deadline = null, CancellationToken cancellationToken = default(CancellationToken),
                    WriteOptions writeOptions = null, ContextPropagationToken propagationToken = null)
 {
     this.headers           = headers;
     this.deadline          = deadline;
     this.cancellationToken = cancellationToken;
     this.writeOptions      = writeOptions;
     this.propagationToken  = propagationToken;
 }
Пример #4
0
 /// <summary>
 /// Creates a new instance of <c>CallOptions</c> struct.
 /// </summary>
 /// <param name="headers">Headers to be sent with the call.</param>
 /// <param name="deadline">Deadline for the call to finish. null means no deadline.</param>
 /// <param name="cancellationToken">Can be used to request cancellation of the call.</param>
 /// <param name="writeOptions">Write options that will be used for this call.</param>
 /// <param name="propagationToken">Context propagation token obtained from <see cref="ServerCallContext"/>.</param>
 public CallOptions(Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken),
                    WriteOptions writeOptions = null, ContextPropagationToken propagationToken = null)
 {
     this.headers = headers;
     this.deadline = deadline;
     this.cancellationToken = cancellationToken;
     this.writeOptions = writeOptions;
     this.propagationToken = propagationToken;
 }
Пример #5
0
 /// <summary>
 /// Creates a new instance of <c>CallOptions</c>.
 /// </summary>
 /// <param name="headers">Headers to be sent with the call.</param>
 /// <param name="deadline">Deadline for the call to finish. null means no deadline.</param>
 /// <param name="cancellationToken">Can be used to request cancellation of the call.</param>
 /// <param name="writeOptions">Write options that will be used for this call.</param>
 /// <param name="propagationToken">Context propagation token obtained from <see cref="ServerCallContext"/>.</param>
 public CallOptions(Metadata headers = null, DateTime? deadline = null, CancellationToken? cancellationToken = null,
                    WriteOptions writeOptions = null, ContextPropagationToken propagationToken = null)
 {
     // TODO(jtattermusch): consider only creating metadata object once it's really needed.
     this.headers = headers ?? new Metadata();
     this.deadline = deadline ?? (propagationToken != null ? propagationToken.Deadline : DateTime.MaxValue);
     this.cancellationToken = cancellationToken ?? (propagationToken != null ? propagationToken.CancellationToken : CancellationToken.None);
     this.writeOptions = writeOptions;
     this.propagationToken = propagationToken;
 }
Пример #6
0
 /// <summary>
 /// Creates a new instance of <c>CallOptions</c> struct.
 /// </summary>
 /// <param name="headers">Headers to be sent with the call.</param>
 /// <param name="deadline">Deadline for the call to finish. null means no deadline.</param>
 /// <param name="cancellationToken">Can be used to request cancellation of the call.</param>
 /// <param name="writeOptions">Write options that will be used for this call.</param>
 /// <param name="propagationToken">Context propagation token obtained from <see cref="ServerCallContext"/>.</param>
 /// <param name="credentials">Credentials to use for this call.</param>
 public CallOptions(Metadata headers          = null, DateTime?deadline = null, GrpcCancellationToken cancellationToken = default(GrpcCancellationToken),
                    WriteOptions writeOptions = null, ContextPropagationToken propagationToken = null, CallCredentials credentials = null)
 {
     this.headers           = headers;
     this.deadline          = deadline;
     this.cancellationToken = cancellationToken;
     this.writeOptions      = writeOptions;
     this.propagationToken  = propagationToken;
     this.credentials       = credentials;
     this.flags             = default(CallFlags);
 }
Пример #7
0
 /// <summary>
 /// Constructs an instance with the specified settings.
 /// </summary>
 /// <param name="cancellationToken">Cancellation token that can be used for cancelling the call.</param>
 /// <param name="credentials">Credentials to use for the call.</param>
 /// <param name="timing"><see cref="CallTiming"/> to use, or null for default retry/expiration behavior.</param>
 /// <param name="headerMutation">Action to modify the headers to send at the beginning of the call.</param>
 /// <param name="writeOptions"><see cref="global::Grpc.Core.WriteOptions"/> that will be used for the call.</param>
 /// <param name="propagationToken"><see cref="ContextPropagationToken"/> for propagating settings from a parent call.</param>
 public CallSettings(
     CancellationToken? cancellationToken,
     CallCredentials credentials,
     CallTiming timing,
     Action<Metadata> headerMutation,
     WriteOptions writeOptions,
     ContextPropagationToken propagationToken)
 {
     CancellationToken = cancellationToken;
     Credentials = credentials;
     Timing = timing;
     HeaderMutation = headerMutation;
     WriteOptions = writeOptions;
     PropagationToken = propagationToken;
 }
Пример #8
0
        public void Normalize()
        {
            Assert.AreSame(Metadata.Empty, new CallOptions().Normalize().Headers);
            Assert.AreEqual(DateTime.MaxValue, new CallOptions().Normalize().Deadline.Value);

            var deadline = DateTime.UtcNow;
            var propagationToken1 = new ContextPropagationToken(CallSafeHandle.NullInstance, deadline, CancellationToken.None,
                new ContextPropagationOptions(propagateDeadline: true, propagateCancellation: false));
            Assert.AreEqual(deadline, new CallOptions(propagationToken: propagationToken1).Normalize().Deadline.Value);
            Assert.Throws(typeof(ArgumentException), () => new CallOptions(deadline: deadline, propagationToken: propagationToken1).Normalize());

            var token = new CancellationTokenSource().Token;
            var propagationToken2 = new ContextPropagationToken(CallSafeHandle.NullInstance, deadline, token,
                new ContextPropagationOptions(propagateDeadline: false, propagateCancellation: true));
            Assert.AreEqual(token, new CallOptions(propagationToken: propagationToken2).Normalize().CancellationToken);
            Assert.Throws(typeof(ArgumentException), () => new CallOptions(cancellationToken: token, propagationToken: propagationToken2).Normalize());
        }
Пример #9
0
 /// <summary>
 /// Returns new instance of <see cref="CallOptions"/> with
 /// <c>PropagationToken</c> set to the value provided. Values of all other fields are preserved.
 /// </summary>
 /// <param name="propagationToken">The context propagation token.</param>
 public CallOptions WithPropagationToken(ContextPropagationToken propagationToken)
 {
     var newOptions = this;
     newOptions.propagationToken = propagationToken;
     return newOptions;
 }