1 #region Copyright notice and license
2 // Copyright 2015 gRPC authors.
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #if GRPC_SUPPORT_WATCH
18 using System.Threading.Channels;
19 using System.Threading.Tasks;
24 namespace Grpc.HealthCheck.Tests
26 internal class TestResponseStreamWriter : IServerStreamWriter<HealthCheckResponse>
28 private readonly Channel<HealthCheckResponse> _channel;
29 private readonly TaskCompletionSource<object> _startTcs;
31 public TestResponseStreamWriter(int maxCapacity = 1, bool started = true)
33 _channel = System.Threading.Channels.Channel.CreateBounded<HealthCheckResponse>(new BoundedChannelOptions(maxCapacity) {
36 FullMode = BoundedChannelFullMode.Wait
40 _startTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
44 public ChannelReader<HealthCheckResponse> WrittenMessagesReader => _channel.Reader;
46 public WriteOptions WriteOptions { get; set; }
48 public async Task WriteAsync(HealthCheckResponse message)
50 if (_startTcs != null)
55 await _channel.Writer.WriteAsync(message);
60 if (_startTcs != null)
62 _startTcs.TrySetResult(null);
66 public void Complete()
68 _channel.Writer.Complete();