• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
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 #endregion
18 
19 using System;
20 using System.Collections.Generic;
21 using System.Threading;
22 using Grpc.Core;
23 using Grpc.Core.Internal;
24 using Grpc.Core.Utils;
25 using NUnit.Framework;
26 
27 namespace Grpc.Core.Tests
28 {
29     public class CallOptionsTest
30     {
31         [Test]
WithMethods()32         public void WithMethods()
33         {
34             var options = new CallOptions();
35 
36             var metadata = new Metadata();
37             Assert.AreSame(metadata, options.WithHeaders(metadata).Headers);
38 
39             var deadline = DateTime.UtcNow;
40             Assert.AreEqual(deadline, options.WithDeadline(deadline).Deadline.Value);
41 
42             var cancellationToken = new CancellationTokenSource().Token;
43             Assert.AreEqual(cancellationToken, options.WithCancellationToken(cancellationToken).CancellationToken);
44 
45             var writeOptions = new WriteOptions();
46             Assert.AreSame(writeOptions, options.WithWriteOptions(writeOptions).WriteOptions);
47 
48             var propagationToken = new ContextPropagationToken(CallSafeHandle.NullInstance, DateTime.UtcNow,
49                 CancellationToken.None, ContextPropagationOptions.Default);
50             Assert.AreSame(propagationToken, options.WithPropagationToken(propagationToken).PropagationToken);
51 
52             var credentials = new FakeCallCredentials();
53             Assert.AreSame(credentials, options.WithCredentials(credentials).Credentials);
54 
55             var flags = CallFlags.WaitForReady | CallFlags.CacheableRequest;
56             Assert.AreEqual(flags, options.WithFlags(flags).Flags);
57 
58             // Check that the original instance is unchanged.
59             Assert.IsNull(options.Headers);
60             Assert.IsNull(options.Deadline);
61             Assert.AreEqual(CancellationToken.None, options.CancellationToken);
62             Assert.IsNull(options.WriteOptions);
63             Assert.IsNull(options.PropagationToken);
64             Assert.IsNull(options.Credentials);
65             Assert.AreEqual(default(CallFlags), options.Flags);
66         }
67 
68         [Test]
Normalize()69         public void Normalize()
70         {
71             Assert.AreSame(Metadata.Empty, new CallOptions().Normalize().Headers);
72             Assert.AreEqual(DateTime.MaxValue, new CallOptions().Normalize().Deadline.Value);
73 
74             var deadline = DateTime.UtcNow;
75             var propagationToken1 = new ContextPropagationToken(CallSafeHandle.NullInstance, deadline, CancellationToken.None,
76                 new ContextPropagationOptions(propagateDeadline: true, propagateCancellation: false));
77             Assert.AreEqual(deadline, new CallOptions(propagationToken: propagationToken1).Normalize().Deadline.Value);
78             Assert.Throws(typeof(ArgumentException), () => new CallOptions(deadline: deadline, propagationToken: propagationToken1).Normalize());
79 
80             var token = new CancellationTokenSource().Token;
81             var propagationToken2 = new ContextPropagationToken(CallSafeHandle.NullInstance, deadline, token,
82                 new ContextPropagationOptions(propagateDeadline: false, propagateCancellation: true));
83             Assert.AreEqual(token, new CallOptions(propagationToken: propagationToken2).Normalize().CancellationToken);
84             Assert.Throws(typeof(ArgumentException), () => new CallOptions(cancellationToken: token, propagationToken: propagationToken2).Normalize());
85         }
86 
87         [Test]
WaitForReady()88         public void WaitForReady()
89         {
90             var callOptions = new CallOptions();
91             Assert.IsFalse(callOptions.IsWaitForReady);
92 
93             Assert.AreEqual(CallFlags.WaitForReady, callOptions.WithWaitForReady().Flags);
94             Assert.IsTrue(callOptions.WithWaitForReady().IsWaitForReady);
95             Assert.IsFalse(callOptions.WithWaitForReady(true).WithWaitForReady(false).IsWaitForReady);
96         }
97     }
98 }
99