aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@users.noreply.github.com>2016-04-24 17:59:32 -0700
committerGravatar Jan Tattermusch <jtattermusch@users.noreply.github.com>2016-04-24 17:59:32 -0700
commita75c63b1dbbbcb4b447d97b4c5eff19dcd3ff126 (patch)
treee72a8d060b79c2e3a88b90c1a95df3b8fb34bc2c /src
parentab8b9f51437ab7502b09bed22b7fc1a265016e44 (diff)
parentefa6af7381a78c67785f1717fa6497e1577f6638 (diff)
Merge pull request #5073 from mleenhardt/calloptions-fluent
CallOptions fluent methods
Diffstat (limited to 'src')
-rw-r--r--src/csharp/Grpc.Core.Tests/CallOptionsTest.cs16
-rw-r--r--src/csharp/Grpc.Core/CallOptions.cs51
2 files changed, 52 insertions, 15 deletions
diff --git a/src/csharp/Grpc.Core.Tests/CallOptionsTest.cs b/src/csharp/Grpc.Core.Tests/CallOptionsTest.cs
index a3a613be74..99a2d47e6e 100644
--- a/src/csharp/Grpc.Core.Tests/CallOptionsTest.cs
+++ b/src/csharp/Grpc.Core.Tests/CallOptionsTest.cs
@@ -54,10 +54,20 @@ namespace Grpc.Core.Tests
var deadline = DateTime.UtcNow;
Assert.AreEqual(deadline, options.WithDeadline(deadline).Deadline.Value);
- var token = new CancellationTokenSource().Token;
- Assert.AreEqual(token, options.WithCancellationToken(token).CancellationToken);
+ 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);
- // Change original instance is unchanged.
+ // Check that the original instance is unchanged.
Assert.IsNull(options.Headers);
Assert.IsNull(options.Deadline);
Assert.AreEqual(CancellationToken.None, options.CancellationToken);
diff --git a/src/csharp/Grpc.Core/CallOptions.cs b/src/csharp/Grpc.Core/CallOptions.cs
index caf8210d91..9ca88849ee 100644
--- a/src/csharp/Grpc.Core/CallOptions.cs
+++ b/src/csharp/Grpc.Core/CallOptions.cs
@@ -100,10 +100,7 @@ namespace Grpc.Core
/// </summary>
public WriteOptions WriteOptions
{
- get
- {
- return this.writeOptions;
- }
+ get { return this.writeOptions; }
}
/// <summary>
@@ -111,10 +108,7 @@ namespace Grpc.Core
/// </summary>
public ContextPropagationToken PropagationToken
{
- get
- {
- return this.propagationToken;
- }
+ get { return this.propagationToken; }
}
/// <summary>
@@ -122,10 +116,7 @@ namespace Grpc.Core
/// </summary>
public CallCredentials Credentials
{
- get
- {
- return this.credentials;
- }
+ get { return this.credentials; }
}
/// <summary>
@@ -165,6 +156,42 @@ namespace Grpc.Core
}
/// <summary>
+ /// Returns new instance of <see cref="CallOptions"/> with
+ /// <c>WriteOptions</c> set to the value provided. Values of all other fields are preserved.
+ /// </summary>
+ /// <param name="writeOptions">The write options.</param>
+ public CallOptions WithWriteOptions(WriteOptions writeOptions)
+ {
+ var newOptions = this;
+ newOptions.writeOptions = writeOptions;
+ return newOptions;
+ }
+
+ /// <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;
+ }
+
+ /// <summary>
+ /// Returns new instance of <see cref="CallOptions"/> with
+ /// <c>Credentials</c> set to the value provided. Values of all other fields are preserved.
+ /// </summary>
+ /// <param name="credentials">The call credentials.</param>
+ public CallOptions WithCredentials(CallCredentials credentials)
+ {
+ var newOptions = this;
+ newOptions.credentials = credentials;
+ return newOptions;
+ }
+
+ /// <summary>
/// Returns a new instance of <see cref="CallOptions"/> with
/// all previously unset values set to their defaults and deadline and cancellation
/// token propagated when appropriate.