• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2019 The 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.Threading.Tasks;
20 using NUnit.Framework;
21 
22 namespace Grpc.Core.Internal.Tests
23 {
24     public class AsyncCallStateTest
25     {
26         [Test]
Stateless()27         public void Stateless()
28         {
29             bool disposed = false;
30             Task<Metadata> responseHeaders = Task.FromResult(new Metadata());
31             Metadata trailers = new Metadata();
32             var state = new AsyncCallState(responseHeaders, () => new Status(StatusCode.DataLoss, "oops"),
33                 () => trailers, () => disposed = true);
34 
35             Assert.AreSame(responseHeaders, state.ResponseHeadersAsync());
36 
37             var status = state.GetStatus();
38             Assert.AreEqual(StatusCode.DataLoss, status.StatusCode);
39             Assert.AreEqual("oops", status.Detail);
40 
41             Assert.AreSame(trailers, state.GetTrailers());
42 
43             Assert.False(disposed);
44             state.Dispose();
45             Assert.True(disposed);
46         }
47 
48         class State
49         {
50             public bool disposed = false;
51             public Task<Metadata> responseHeaders = Task.FromResult(new Metadata());
52             public Metadata trailers = new Metadata();
53             public Status status = new Status(StatusCode.DataLoss, "oops");
Dispose()54             public void Dispose() { disposed = true; }
55         }
56 
57         [Test]
WithState()58         public void WithState()
59         {
60             var callbackState = new State();
61 
62             var state = new AsyncCallState(
63                 obj => ((State)obj).responseHeaders,
64                 obj => ((State)obj).status,
65                 obj => ((State)obj).trailers,
66                 obj => ((State)obj).Dispose(),
67                 callbackState);
68 
69             Assert.AreSame(callbackState.responseHeaders, state.ResponseHeadersAsync());
70 
71             var status = state.GetStatus();
72             Assert.AreEqual(StatusCode.DataLoss, status.StatusCode);
73             Assert.AreEqual("oops", status.Detail);
74 
75             Assert.AreSame(callbackState.trailers, state.GetTrailers());
76 
77             Assert.False(callbackState.disposed);
78             state.Dispose();
79             Assert.True(callbackState.disposed);
80         }
81     }
82 }
83