Skip to content

Commit a754ac0

Browse files
Michał Wójciktimbussmann
authored andcommitted
Make HandleCurrentMessageLater obsolete (#138)
* Removing the NSB.Testing.Fakes as a source packages and copying the code base into the project * Adding obsoletes to HandleCurrentMessageLater * Change to the approved API to include the obsoletes * Moving obsoletes into obsolete.cs
1 parent f776a7e commit a754ac0

40 files changed

+1491
-1
lines changed

src/NServiceBus.Testing.Tests/ApprovalFiles/APIApprovals.Approve.approved.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ namespace NServiceBus.Testing
233233
{
234234
public TestableInvokeHandlerContext(NServiceBus.IMessageCreator messageCreator = null) { }
235235
public bool DoNotContinueDispatchingCurrentMessageToHandlersWasCalled { get; set; }
236+
[System.ObsoleteAttribute("HandleCurrentMessageLater has been deprecated. The member currently throws a NotI" +
237+
"mplementedException. Will be removed in version 8.0.0.", true)]
236238
public bool HandleCurrentMessageLaterWasCalled { get; }
237239
public bool HandlerInvocationAborted { get; }
238240
public System.Collections.Generic.Dictionary<string, string> Headers { get; set; }
@@ -241,6 +243,8 @@ namespace NServiceBus.Testing
241243
public NServiceBus.Unicast.Messages.MessageMetadata MessageMetadata { get; set; }
242244
public NServiceBus.Persistence.SynchronizedStorageSession SynchronizedStorageSession { get; set; }
243245
public void DoNotContinueDispatchingCurrentMessageToHandlers() { }
246+
[System.ObsoleteAttribute("HandleCurrentMessageLater has been deprecated. The member currently throws a NotI" +
247+
"mplementedException. Will be removed in version 8.0.0.", true)]
244248
public System.Threading.Tasks.Task HandleCurrentMessageLater() { }
245249
}
246250
public class TestableMessageHandlerContext : NServiceBus.Testing.TestableInvokeHandlerContext
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace NServiceBus.Testing
2+
{
3+
using System;
4+
using System.IO;
5+
using Logging;
6+
7+
class DefaultTestingLoggerFactory : ILoggerFactory
8+
{
9+
public DefaultTestingLoggerFactory(LogLevel filterLevel, TextWriter textWriter)
10+
{
11+
this.filterLevel = filterLevel;
12+
textWriterLogger = new TextWriterLogger(textWriter);
13+
isDebugEnabled = LogLevel.Debug >= filterLevel;
14+
isInfoEnabled = LogLevel.Info >= filterLevel;
15+
isWarnEnabled = LogLevel.Warn >= filterLevel;
16+
isErrorEnabled = LogLevel.Error >= filterLevel;
17+
isFatalEnabled = LogLevel.Fatal >= filterLevel;
18+
}
19+
20+
public ILog GetLogger(Type type)
21+
{
22+
return GetLogger(type.FullName);
23+
}
24+
25+
public ILog GetLogger(string name)
26+
{
27+
return new NamedLogger(name, this)
28+
{
29+
IsDebugEnabled = isDebugEnabled,
30+
IsInfoEnabled = isInfoEnabled,
31+
IsWarnEnabled = isWarnEnabled,
32+
IsErrorEnabled = isErrorEnabled,
33+
IsFatalEnabled = isFatalEnabled
34+
};
35+
}
36+
37+
public void Write(string name, LogLevel messageLevel, string message)
38+
{
39+
if (messageLevel < filterLevel)
40+
{
41+
return;
42+
}
43+
var datePart = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
44+
var paddedLevel = messageLevel.ToString().ToUpper().PadRight(5);
45+
var fullMessage = $"{datePart} {paddedLevel} {name} {message}";
46+
lock (locker)
47+
{
48+
textWriterLogger.Write(fullMessage);
49+
}
50+
}
51+
52+
LogLevel filterLevel;
53+
bool isDebugEnabled;
54+
bool isErrorEnabled;
55+
bool isFatalEnabled;
56+
bool isInfoEnabled;
57+
bool isWarnEnabled;
58+
59+
object locker = new object();
60+
TextWriterLogger textWriterLogger;
61+
}
62+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// ReSharper disable PartialTypeWithSinglePart
2+
namespace NServiceBus.Testing
3+
{
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using ObjectBuilder;
8+
9+
/// <summary>
10+
/// A fake implementation of <see cref="IBuilder" /> for testing purposes.
11+
/// </summary>
12+
public partial class FakeBuilder : IBuilder
13+
{
14+
/// <summary>
15+
/// Returns an instantiation of the given type.
16+
/// </summary>
17+
/// <param name="typeToBuild">The <see cref="T:System.Type" /> to build.</param>
18+
/// <returns>
19+
/// The component instance.
20+
/// </returns>
21+
public virtual object Build(Type typeToBuild)
22+
{
23+
return BuildAll(typeToBuild).First();
24+
}
25+
26+
/// <summary>
27+
/// Creates an instance of the given type, injecting it with all defined dependencies.
28+
/// </summary>
29+
/// <typeparam name="T">Type to be resolved.</typeparam>
30+
/// <returns>
31+
/// Instance of <typeparamref name="T" />.
32+
/// </returns>
33+
public virtual T Build<T>()
34+
{
35+
return BuildAll<T>().First();
36+
}
37+
38+
/// <summary>
39+
/// For each type that is compatible with the given type, an instance is created with all dependencies injected.
40+
/// </summary>
41+
/// <param name="typeToBuild">The <see cref="T:System.Type" /> to build.</param>
42+
/// <returns>
43+
/// The component instances.
44+
/// </returns>
45+
public virtual IEnumerable<object> BuildAll(Type typeToBuild)
46+
{
47+
if (instances.ContainsKey(typeToBuild))
48+
{
49+
return instances[typeToBuild];
50+
}
51+
52+
if (factories.ContainsKey(typeToBuild))
53+
{
54+
return factories[typeToBuild]();
55+
}
56+
57+
throw new Exception($"Cannot build instance of type {typeToBuild} because there was no instance of factory registered for it.");
58+
}
59+
60+
/// <summary>
61+
/// For each type that is compatible with T, an instance is created with all dependencies injected, and yielded to the
62+
/// caller.
63+
/// </summary>
64+
/// <typeparam name="T">Type to be resolved.</typeparam>
65+
/// <returns>
66+
/// Instances of <typeparamref name="T" />.
67+
/// </returns>
68+
public virtual IEnumerable<T> BuildAll<T>()
69+
{
70+
var type = typeof(T);
71+
72+
if (instances.ContainsKey(type))
73+
{
74+
return instances[type].Cast<T>();
75+
}
76+
77+
if (factories.ContainsKey(type))
78+
{
79+
return factories[type]().Cast<T>();
80+
}
81+
82+
return Enumerable.Empty<T>();
83+
}
84+
85+
/// <summary>
86+
/// Builds an instance of the defined type injecting it with all defined dependencies
87+
/// and invokes the given action on the instance.
88+
/// </summary>
89+
/// <param name="typeToBuild">The <see cref="T:System.Type" /> to build.</param>
90+
/// <param name="action">The callback to call.</param>
91+
public virtual void BuildAndDispatch(Type typeToBuild, Action<object> action)
92+
{
93+
throw new NotImplementedException();
94+
}
95+
96+
/// <summary>
97+
/// Returns a child instance of the container to facilitate deterministic disposal
98+
/// of all resources built by the child container.
99+
/// </summary>
100+
/// <returns>
101+
/// Returns a new child container.
102+
/// </returns>
103+
public virtual IBuilder CreateChildBuilder()
104+
{
105+
return this;
106+
}
107+
108+
/// <summary>
109+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
110+
/// </summary>
111+
/// <filterpriority>2</filterpriority>
112+
public virtual void Dispose()
113+
{
114+
}
115+
116+
/// <summary>
117+
/// Releases a component instance.
118+
/// </summary>
119+
/// <param name="instance">The component instance to release.</param>
120+
public virtual void Release(object instance)
121+
{
122+
}
123+
124+
/// <summary>
125+
/// Registers an instance which will be returned every time an instance of <typeparamref name="T" /> is resolved.
126+
/// </summary>
127+
public void Register<T>(params T[] instance) where T : class
128+
{
129+
instances.Add(typeof(T), instance);
130+
}
131+
132+
/// <summary>
133+
/// Registers a factory method which will be invoked every time an instance of <typeparamref name="T" /> is resolved.
134+
/// </summary>
135+
public void Register<T>(Func<T> factory)
136+
{
137+
factories.Add(typeof(T), () => new object[]
138+
{
139+
factory()
140+
});
141+
}
142+
143+
/// <summary>
144+
/// Registers a factory method which will be invoked every time an instance of <typeparamref name="T" /> is resolved.
145+
/// </summary>
146+
public void Register<T>(Func<T[]> factory) where T : class
147+
{
148+
factories.Add(typeof(T), factory);
149+
}
150+
151+
Dictionary<Type, Func<object[]>> factories = new Dictionary<Type, Func<object[]>>();
152+
153+
Dictionary<Type, object[]> instances = new Dictionary<Type, object[]>();
154+
}
155+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
namespace NServiceBus.Testing
2+
{
3+
using System;
4+
using Logging;
5+
6+
class NamedLogger : ILog
7+
{
8+
public NamedLogger(string name, DefaultTestingLoggerFactory defaultLoggerFactory)
9+
{
10+
this.name = name;
11+
this.defaultLoggerFactory = defaultLoggerFactory;
12+
}
13+
14+
public bool IsDebugEnabled { get; internal set; }
15+
public bool IsInfoEnabled { get; internal set; }
16+
public bool IsWarnEnabled { get; internal set; }
17+
public bool IsErrorEnabled { get; internal set; }
18+
public bool IsFatalEnabled { get; internal set; }
19+
20+
public void Debug(string message)
21+
{
22+
defaultLoggerFactory.Write(name, LogLevel.Debug, message);
23+
}
24+
25+
public void Debug(string message, Exception exception)
26+
{
27+
defaultLoggerFactory.Write(name, LogLevel.Debug, message + Environment.NewLine + exception);
28+
}
29+
30+
public void DebugFormat(string format, params object[] args)
31+
{
32+
defaultLoggerFactory.Write(name, LogLevel.Debug, string.Format(format, args));
33+
}
34+
35+
public void Info(string message)
36+
{
37+
defaultLoggerFactory.Write(name, LogLevel.Info, message);
38+
}
39+
40+
public void Info(string message, Exception exception)
41+
{
42+
defaultLoggerFactory.Write(name, LogLevel.Info, message + Environment.NewLine + exception);
43+
}
44+
45+
public void InfoFormat(string format, params object[] args)
46+
{
47+
defaultLoggerFactory.Write(name, LogLevel.Info, string.Format(format, args));
48+
}
49+
50+
public void Warn(string message)
51+
{
52+
defaultLoggerFactory.Write(name, LogLevel.Warn, message);
53+
}
54+
55+
public void Warn(string message, Exception exception)
56+
{
57+
defaultLoggerFactory.Write(name, LogLevel.Warn, message + Environment.NewLine + exception);
58+
}
59+
60+
public void WarnFormat(string format, params object[] args)
61+
{
62+
defaultLoggerFactory.Write(name, LogLevel.Warn, string.Format(format, args));
63+
}
64+
65+
public void Error(string message)
66+
{
67+
defaultLoggerFactory.Write(name, LogLevel.Error, message);
68+
}
69+
70+
public void Error(string message, Exception exception)
71+
{
72+
defaultLoggerFactory.Write(name, LogLevel.Error, message + Environment.NewLine + exception);
73+
}
74+
75+
public void ErrorFormat(string format, params object[] args)
76+
{
77+
defaultLoggerFactory.Write(name, LogLevel.Error, string.Format(format, args));
78+
}
79+
80+
public void Fatal(string message)
81+
{
82+
defaultLoggerFactory.Write(name, LogLevel.Fatal, message);
83+
}
84+
85+
public void Fatal(string message, Exception exception)
86+
{
87+
defaultLoggerFactory.Write(name, LogLevel.Error, message + Environment.NewLine + exception);
88+
}
89+
90+
public void FatalFormat(string format, params object[] args)
91+
{
92+
defaultLoggerFactory.Write(name, LogLevel.Fatal, string.Format(format, args));
93+
}
94+
95+
DefaultTestingLoggerFactory defaultLoggerFactory;
96+
string name;
97+
}
98+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace NServiceBus.Testing
2+
{
3+
using Extensibility;
4+
5+
/// <summary>
6+
/// Represents an outgoing message. Contains the message itself and its associated options.
7+
/// </summary>
8+
/// <typeparam name="TMessage">The message type.</typeparam>
9+
/// <typeparam name="TOptions">The options type of the message.</typeparam>
10+
public class OutgoingMessage<TMessage, TOptions> where TOptions : ExtendableOptions
11+
{
12+
/// <summary>
13+
/// Creates a new instance for the given message and options.
14+
/// </summary>
15+
protected OutgoingMessage(TMessage message, TOptions options)
16+
{
17+
Message = message;
18+
Options = options;
19+
}
20+
21+
/// <summary>
22+
/// The outgoing message.
23+
/// </summary>
24+
public TMessage Message { get; }
25+
26+
/// <summary>
27+
/// The options of the outgoing message.
28+
/// </summary>
29+
public TOptions Options { get; }
30+
}
31+
}

0 commit comments

Comments
 (0)