Jan Tattermusch | 3d45afe | 2015-12-10 18:11:22 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | 3d45afe | 2015-12-10 18:11:22 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jan Tattermusch | 3d45afe | 2015-12-10 18:11:22 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 3d45afe | 2015-12-10 18:11:22 -0800 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | 3d45afe | 2015-12-10 18:11:22 -0800 | [diff] [blame] | 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 | |
Jan Tattermusch | de0d8b5 | 2015-12-10 18:53:55 -0800 | [diff] [blame] | 27 | namespace Grpc.Core.Tests |
Jan Tattermusch | 3d45afe | 2015-12-10 18:11:22 -0800 | [diff] [blame] | 28 | { |
| 29 | public class CallOptionsTest |
| 30 | { |
| 31 | [Test] |
| 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 | |
Mathieu Leenhardt | 9ef407b | 2016-02-04 23:24:30 +0100 | [diff] [blame] | 42 | var cancellationToken = new CancellationTokenSource().Token; |
| 43 | Assert.AreEqual(cancellationToken, options.WithCancellationToken(cancellationToken).CancellationToken); |
Jan Tattermusch | 3d45afe | 2015-12-10 18:11:22 -0800 | [diff] [blame] | 44 | |
Mathieu Leenhardt | 9ef407b | 2016-02-04 23:24:30 +0100 | [diff] [blame] | 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 | |
Jan Tattermusch | 0faf80c | 2016-11-22 16:05:07 +0100 | [diff] [blame] | 55 | var flags = CallFlags.WaitForReady | CallFlags.CacheableRequest; |
| 56 | Assert.AreEqual(flags, options.WithFlags(flags).Flags); |
| 57 | |
Mathieu Leenhardt | 9ef407b | 2016-02-04 23:24:30 +0100 | [diff] [blame] | 58 | // Check that the original instance is unchanged. |
Jan Tattermusch | 3d45afe | 2015-12-10 18:11:22 -0800 | [diff] [blame] | 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); |
Jan Tattermusch | 0faf80c | 2016-11-22 16:05:07 +0100 | [diff] [blame] | 65 | Assert.AreEqual(default(CallFlags), options.Flags); |
Jan Tattermusch | 3d45afe | 2015-12-10 18:11:22 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | [Test] |
| 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 | } |
Jan Tattermusch | 058f555 | 2016-12-02 16:52:26 +0100 | [diff] [blame] | 86 | |
| 87 | [Test] |
| 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 | } |
Jan Tattermusch | 3d45afe | 2015-12-10 18:11:22 -0800 | [diff] [blame] | 97 | } |
| 98 | } |