blob: 8e5c411caddd6e730cde50ea1613423e40d4ffa2 [file] [log] [blame]
Jan Tattermusch3d45afe2015-12-10 18:11:22 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermusch3d45afe2015-12-10 18:11:22 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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 Tattermusch3d45afe2015-12-10 18:11:22 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch3d45afe2015-12-10 18:11:22 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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 Tattermusch3d45afe2015-12-10 18:11:22 -080016
17#endregion
18
19using System;
20using System.Collections.Generic;
21using System.Threading;
22using Grpc.Core;
23using Grpc.Core.Internal;
24using Grpc.Core.Utils;
25using NUnit.Framework;
26
Jan Tattermuschde0d8b52015-12-10 18:53:55 -080027namespace Grpc.Core.Tests
Jan Tattermusch3d45afe2015-12-10 18:11:22 -080028{
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 Leenhardt9ef407b2016-02-04 23:24:30 +010042 var cancellationToken = new CancellationTokenSource().Token;
43 Assert.AreEqual(cancellationToken, options.WithCancellationToken(cancellationToken).CancellationToken);
Jan Tattermusch3d45afe2015-12-10 18:11:22 -080044
Mathieu Leenhardt9ef407b2016-02-04 23:24:30 +010045 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 Tattermusch0faf80c2016-11-22 16:05:07 +010055 var flags = CallFlags.WaitForReady | CallFlags.CacheableRequest;
56 Assert.AreEqual(flags, options.WithFlags(flags).Flags);
57
Mathieu Leenhardt9ef407b2016-02-04 23:24:30 +010058 // Check that the original instance is unchanged.
Jan Tattermusch3d45afe2015-12-10 18:11:22 -080059 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 Tattermusch0faf80c2016-11-22 16:05:07 +010065 Assert.AreEqual(default(CallFlags), options.Flags);
Jan Tattermusch3d45afe2015-12-10 18:11:22 -080066 }
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 Tattermusch058f5552016-12-02 16:52:26 +010086
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 Tattermusch3d45afe2015-12-10 18:11:22 -080097 }
98}