forked from confluentinc/confluent-kafka-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoqExample.cs
73 lines (63 loc) · 2.98 KB
/
MoqExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2018 Confluent Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Refer to LICENSE for more information.
using System;
using Xunit;
using Moq;
namespace Confluent.Kafka.UnitTests
{
/// <summary>
/// An example of using Moq with IProducer.
/// (not a test of Confluent.Kafka as such)
/// </summary>
public class MoqExampleTests
{
[Fact]
public void IProducer()
{
// Error storedProducerError = new Error(ErrorCode.NoError);
// var mock = new Mock<IProducer<Null, string>>();
// var producer = mock.Object;
// producer.OnError += (_, e)
// => storedProducerError = e;
// var produceCount = 0;
// var flushCount = 0;
// mock.Setup(m => m.Produce(It.IsAny<string>(), It.IsAny<Message<Null, string>>(), It.IsAny<Action<DeliveryReport<Null, string>>>()))
// .Callback<string, Message<Null, string>, Action<DeliveryReport<Null, string>>>((topic, message, action) =>
// {
// var result = new DeliveryReport<Null, string>
// {
// Topic = topic, Partition = 0, Offset = 0, Error = new Error(ErrorCode.NoError),
// Message = message
// };
// // Note: this is a simplification of the actual Producer implementation -
// // A good mock would delay invocation of the callback and invoke it on a
// // different thread.
// action.Invoke(result);
// produceCount += 1;
// });
// mock.Setup(m => m.Flush(It.IsAny<TimeSpan>())).Returns(0).Callback(() => flushCount += 1);
// DeliveryReport<Null, string> produced = null;
// producer.Produce("my-topic", new Message<Null, string> { Value = "my-value" }, (m) => produced = m);
// var remaining = producer.Flush(TimeSpan.FromSeconds(10));
// Assert.Equal("my-topic", produced.Topic);
// Assert.Equal(1, produceCount);
// Assert.Equal(1, flushCount);
// Assert.Equal(0, remaining);
// mock.Raise(m => m.OnError += null, new object[] { new object(), new Error(ErrorCode.NotController) });
// Assert.Equal(storedProducerError, new Error(ErrorCode.NotController));
}
}
}