Commit fc5cffa
Changed files (31)
product
Gorilla.Commons.Infrastructure
Threading
Gorilla.Commons.Infrastructure.ThirdParty
product/Gorilla.Commons.Infrastructure/Eventing/EventAggregator.cs
@@ -1,66 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq.Expressions;
-using System.Threading;
-using Gorilla.Commons.Utility.Extensions;
-
-namespace Gorilla.Commons.Infrastructure.Eventing
-{
- public interface IEventAggregator
- {
- void subscribe_to<Event>(IEventSubscriber<Event> subscriber) where Event : IEvent;
- void subscribe<Listener>(Listener subscriber) where Listener : class;
- void publish<Event>(Event the_event_to_broadcast) where Event : IEvent;
- void publish<T>(Expression<Action<T>> call) where T : class;
- void publish<Event>() where Event : IEvent, new();
- }
-
- public class EventAggregator : IEventAggregator
- {
- readonly SynchronizationContext context;
- readonly HashSet<object> subscribers;
- readonly object mutex;
-
- public EventAggregator(SynchronizationContext context)
- {
- subscribers = new HashSet<object>();
- mutex = new object();
- this.context = context;
- }
-
- public void subscribe_to<Event>(IEventSubscriber<Event> subscriber) where Event : IEvent
- {
- subscribe(subscriber);
- }
-
- public void subscribe<Listener>(Listener subscriber) where Listener : class
- {
- within_lock(() => subscribers.Add(subscriber));
- }
-
- public void publish<Event>(Event the_event_to_broadcast) where Event : IEvent
- {
- process(() => subscribers.call_on_each<IEventSubscriber<Event>>(x => x.notify(the_event_to_broadcast)));
- }
-
- public void publish<T>(Expression<Action<T>> call) where T : class
- {
- process(() => subscribers.each(x => x.call_on(call.Compile())));
- }
-
- public void publish<Event>() where Event : IEvent, new()
- {
- publish(new Event());
- }
-
- void within_lock(Action action)
- {
- lock (mutex) action();
- }
-
- void process(Action action)
- {
- context.Send(x => action(), new object());
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Eventing/EventAggregatorSpecs.cs
@@ -1,79 +0,0 @@
-using System.Data;
-using System.Threading;
-using developwithpassion.bdd.contexts;
-using Gorilla.Commons.Testing;
-using Rhino.Mocks;
-
-namespace Gorilla.Commons.Infrastructure.Eventing
-{
- public abstract class behaves_like_event_aggregator : concerns_for<IEventAggregator, EventAggregator>
- {
- public override IEventAggregator create_sut()
- {
- return new EventAggregator(new SynchronizationContext());
- }
- }
-
- [Concern(typeof(EventAggregator))]
- public class when_a_event_is_raised_in_the_system : behaves_like_event_aggregator
- {
- it should_notify_all_subscribers_of_the_event = () =>
- {
- first_subscriber.was_told_to<IEventSubscriber<TestEvent>>(x => x.notify(message));
- second_subscriber.was_told_to(x => x.notify(message));
- };
-
- it should_not_notify_any_subscribers_that_subscribed_to_a_different_event =
- () => incorrect_subscriber.was_not_told_to(x => x.notify(Arg<AnotherEvent>.Is.Anything));
-
- context c = () =>
- {
- message = new TestEvent();
- first_subscriber = an<IEventSubscriber<TestEvent>>();
- second_subscriber = an<IEventSubscriber<TestEvent>>();
- incorrect_subscriber = an<IEventSubscriber<AnotherEvent>>();
- };
-
- because b = () =>
- {
- sut.subscribe_to(first_subscriber);
- sut.subscribe(second_subscriber);
- sut.publish(message);
- };
-
- static TestEvent message;
- static IEventSubscriber<TestEvent> first_subscriber;
- static IEventSubscriber<TestEvent> second_subscriber;
- static IEventSubscriber<AnotherEvent> incorrect_subscriber;
- }
-
- [Concern(typeof(EventAggregator))]
- public class when_publishing_a_call_to_all_subscribers : behaves_like_event_aggregator
- {
- it should_make_the_call_on_each_subscriber = () => connection.was_told_to(x => x.ChangeDatabase("localhost"));
-
- context c = () =>
- {
- connection = an<IDbConnection>();
- command = an<IDbCommand>();
- };
-
- because b = () =>
- {
- sut.subscribe(connection);
- sut.subscribe(command);
- sut.publish<IDbConnection>(x => x.ChangeDatabase("localhost"));
- };
-
- static IDbConnection connection;
- static IDbCommand command;
- }
-
- public class TestEvent : IEvent
- {
- }
-
- public class AnotherEvent : IEvent
- {
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Eventing/IEvent.cs
@@ -1,6 +0,0 @@
-namespace Gorilla.Commons.Infrastructure.Eventing
-{
- public interface IEvent
- {
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Eventing/IEventSubscriber.cs
@@ -1,7 +0,0 @@
-namespace Gorilla.Commons.Infrastructure.Eventing
-{
- public interface IEventSubscriber<Event> where Event : IEvent
- {
- void notify(Event message);
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/AsynchronousCommandProcessor.cs
@@ -1,90 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq.Expressions;
-using System.Threading;
-using Gorilla.Commons.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public class AsynchronousCommandProcessor : ICommandProcessor
- {
- readonly Queue<ICommand> queued_commands;
- readonly EventWaitHandle manual_reset;
- readonly IList<Thread> worker_threads;
- bool keep_working;
-
- public AsynchronousCommandProcessor()
- {
- queued_commands = new Queue<ICommand>();
- worker_threads = new List<Thread>();
- manual_reset = new ManualResetEvent(false);
- }
-
- public void add(Expression<Action> action_to_process)
- {
- add(new ActionCommand(action_to_process));
- }
-
- public void add(ICommand command_to_process)
- {
- lock (queued_commands)
- {
- if (queued_commands.Contains(command_to_process)) return;
- queued_commands.Enqueue(command_to_process);
- reset_thread();
- }
- }
-
- public void run()
- {
- reset_thread();
- keep_working = true;
- var worker_thread = new Thread(run_commands);
- worker_thread.SetApartmentState(ApartmentState.STA);
- worker_threads.Add(worker_thread);
- worker_thread.Start();
- }
-
- public void stop()
- {
- keep_working = false;
- manual_reset.Set();
- //manual_reset.Close();
- }
-
- [STAThread]
- void run_commands()
- {
- while (keep_working)
- {
- manual_reset.WaitOne();
- run_next_command();
- }
- }
-
- void run_next_command()
- {
- ICommand command;
- lock (queued_commands)
- {
- if (queued_commands.Count == 0)
- {
- manual_reset.Reset();
- return;
- }
- command = queued_commands.Dequeue();
- }
- command.run();
- reset_thread();
- }
-
- void reset_thread()
- {
- lock (queued_commands)
- {
- if (queued_commands.Count > 0) manual_reset.Set();
- else manual_reset.Reset();
- }
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/BackgroundThread.cs
@@ -1,34 +0,0 @@
-using MoMoney.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public interface IBackgroundThread : IDisposableCommand
- {
- }
-
- public class BackgroundThread : IBackgroundThread
- {
- readonly IWorkerThread worker_thread;
-
- public BackgroundThread(IDisposableCommand command_to_execute) : this(command_to_execute, new WorkerThread())
- {
- }
-
- public BackgroundThread(IDisposableCommand command_to_execute, IWorkerThread worker_thread)
- {
- this.worker_thread = worker_thread;
- worker_thread.DoWork += (sender, e) => command_to_execute.run();
- worker_thread.Disposed += (sender, e) => command_to_execute.Dispose();
- }
-
- public void run()
- {
- worker_thread.begin();
- }
-
- public void Dispose()
- {
- worker_thread.Dispose();
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/BackgroundThreadFactory.cs
@@ -1,33 +0,0 @@
-using System;
-using Gorilla.Commons.Infrastructure.Container;
-using Gorilla.Commons.Utility.Core;
-using MoMoney.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public interface IBackgroundThreadFactory
- {
- IBackgroundThread create_for<CommandToExecute>() where CommandToExecute : IDisposableCommand;
- IBackgroundThread create_for(Action action);
- }
-
- public class BackgroundThreadFactory : IBackgroundThreadFactory
- {
- private readonly IDependencyRegistry registry;
-
- public BackgroundThreadFactory(IDependencyRegistry registry)
- {
- this.registry = registry;
- }
-
- public IBackgroundThread create_for<CommandToExecute>() where CommandToExecute : IDisposableCommand
- {
- return new BackgroundThread(registry.get_a<CommandToExecute>());
- }
-
- public IBackgroundThread create_for(Action action)
- {
- return new BackgroundThread(new DisposableCommand(action));
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/BackgroundThreadFactorySpecs.cs
@@ -1,28 +0,0 @@
-using developwithpassion.bdd.contexts;
-using Gorilla.Commons.Infrastructure.Container;
-using Gorilla.Commons.Testing;
-using MoMoney.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- [Concern(typeof (BackgroundThreadFactory))]
- public abstract class behaves_like_a_background_thread_factory : concerns_for<IBackgroundThreadFactory, BackgroundThreadFactory>
- {
- context c = () => { registry = the_dependency<IDependencyRegistry>(); };
-
- protected static IDependencyRegistry registry;
- }
-
- [Concern(typeof (BackgroundThreadFactory))]
- public class when_creating_a_background_thread : behaves_like_a_background_thread_factory
- {
- it should_return_an_instance_of_a_background_thread = () => result.should_not_be_null();
-
- it should_lookup_an_instance_of_the_command_to_execute =
- () => registry.was_told_to(r => r.get_a<IDisposableCommand>());
-
- because b = () => { result = sut.create_for<IDisposableCommand>(); };
-
- static IBackgroundThread result;
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/BackgroundThreadSpecs.cs
@@ -1,42 +0,0 @@
-using developwithpassion.bdd.contexts;
-using Gorilla.Commons.Testing;
-using MoMoney.Utility.Core;
-using Rhino.Mocks;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- [Concern(typeof (BackgroundThread))]
- public abstract class behaves_like_a_background_thread : concerns_for<IBackgroundThread, BackgroundThread>
- {
- context c = () =>
- {
- command_to_execute = the_dependency<IDisposableCommand>();
- worker_thread = the_dependency<IWorkerThread>();
- };
-
- protected static IDisposableCommand command_to_execute;
- protected static IWorkerThread worker_thread;
- }
-
- [Concern(typeof (BackgroundThread))]
- public class when_executing_a_command_on_a_background_thread : behaves_like_a_background_thread
- {
- it should_execute_the_command_asynchronously = () => command_to_execute.was_told_to(c => c.run());
-
- it should_start_the_worker_thread_asynchronously = () => worker_thread.was_told_to(t => t.begin());
-
- because b = () =>
- {
- sut.run();
- worker_thread.Raise(t => t.DoWork += null, null, null);
- };
- }
-
- [Concern(typeof (BackgroundThread))]
- public class when_disposing_a_background_thread : behaves_like_a_background_thread
- {
- it should_dispose_the_command_running_on_the_thread = () => worker_thread.was_told_to(w => w.Dispose());
-
- because b = () => sut.Dispose();
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/CommandProcessor.cs
@@ -1,37 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq.Expressions;
-using Gorilla.Commons.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public class CommandProcessor : ICommandProcessor
- {
- readonly Queue<ICommand> queued_commands;
-
- public CommandProcessor()
- {
- queued_commands = new Queue<ICommand>();
- }
-
- public void add(Expression<Action> action_to_process)
- {
- add(new ActionCommand(action_to_process));
- }
-
- public void add(ICommand command_to_process)
- {
- queued_commands.Enqueue(command_to_process);
- }
-
- public void run()
- {
- while (queued_commands.Count > 0) queued_commands.Dequeue().run();
- }
-
- public void stop()
- {
- queued_commands.Clear();
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/CommandProcessorSpecs.cs
@@ -1,53 +0,0 @@
-using developwithpassion.bdd.contexts;
-using Gorilla.Commons.Testing;
-using Gorilla.Commons.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- [Concern(typeof (CommandProcessor))]
- public abstract class behaves_like_a_command_processor : concerns_for<ICommandProcessor, CommandProcessor>
- {
- }
-
- [Concern(typeof (CommandProcessor))]
- public class when_running_all_the_queued_commands_waiting_for_execution : behaves_like_a_command_processor
- {
- it should_run_the_first_command_in_the_queue = () => first_command.was_told_to(f => f.run());
-
- it should_run_the_second_command_in_the_queue = () => second_command.was_told_to(f => f.run());
-
- context c = () =>
- {
- first_command = an<ICommand>();
- second_command = an<ICommand>();
- };
-
- because b = () =>
- {
- sut.add(first_command);
- sut.add(second_command);
- sut.run();
- };
-
- static ICommand first_command;
- static ICommand second_command;
- }
-
- [Concern(typeof (CommandProcessor))]
- public class when_attempting_to_rerun_the_command_processor : behaves_like_a_command_processor
- {
- it should_not_re_run_the_commands_that_have_already_executed =
- () => first_command.was_told_to(f => f.run()).only_once();
-
- context c = () => { first_command = an<ICommand>(); };
-
- because b = () =>
- {
- sut.add(first_command);
- sut.run();
- sut.run();
- };
-
- static ICommand first_command;
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/ICommandProcessor.cs
@@ -1,13 +0,0 @@
-using System;
-using System.Linq.Expressions;
-using Gorilla.Commons.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public interface ICommandProcessor : ICommand
- {
- void add(Expression<Action> action_to_process);
- void add(ICommand command_to_process);
- void stop();
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/IntervalTimer.cs
@@ -1,45 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Timers;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public interface ITimer
- {
- void start_notifying(ITimerClient client_to_be_notified, TimeSpan span);
- void stop_notifying(ITimerClient client_to_stop_notifying);
- }
-
- public class IntervalTimer : ITimer
- {
- readonly ITimerFactory factory;
- readonly IDictionary<ITimerClient, Timer> timers;
-
- public IntervalTimer() : this(new TimerFactory())
- {
- }
-
- public IntervalTimer(ITimerFactory factory)
- {
- this.factory = factory;
- timers = new Dictionary<ITimerClient, Timer>();
- }
-
- public void start_notifying(ITimerClient client_to_be_notified, TimeSpan span)
- {
- stop_notifying(client_to_be_notified);
-
- var timer = factory.create_for(span);
- timer.Elapsed += (sender, args) => client_to_be_notified.notify();
- timer.Start();
- timers[client_to_be_notified] = timer;
- }
-
- public void stop_notifying(ITimerClient client_to_stop_notifying)
- {
- if (!timers.ContainsKey(client_to_stop_notifying)) return;
- timers[client_to_stop_notifying].Stop();
- timers[client_to_stop_notifying].Dispose();
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/IntervalTimerSpecs.cs
@@ -1,130 +0,0 @@
-using System;
-using System.Timers;
-using developwithpassion.bdd.contexts;
-using Gorilla.Commons.Testing;
-using Rhino.Mocks;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- [Concern(typeof (IntervalTimer))]
- public abstract class behaves_like_an_interval_timer : concerns_for<ITimer, IntervalTimer>
- {
- context c = () => { factory = the_dependency<ITimerFactory>(); };
-
- protected static ITimerFactory factory;
- }
-
- [Concern(typeof (IntervalTimer))]
- public class when_starting_a_timer_for_a_new_client : behaves_like_an_interval_timer
- {
- static ITimerClient client;
- static Timer timer;
-
- it should_create_a_new_timer = () => factory.was_told_to(f => f.create_for(new TimeSpan(0, 10, 0)));
-
- it should_start_the_timer = () => timer.was_told_to(t => t.Start());
-
- context c = () =>
- {
- client = an<ITimerClient>();
- timer = dependency<Timer>();
-
- factory.is_told_to(f => f.create_for(new TimeSpan(0, 10, 0))).it_will_return(timer);
- };
-
- because b = () => sut.start_notifying(client, new TimeSpan(0, 10, 0));
- }
-
- [Concern(typeof (IntervalTimer))]
- public class when_starting_a_timer_for_an_existing_client : behaves_like_an_interval_timer
- {
- it should_stop_the_previously_started_timer = () =>
- {
- first_timer.was_told_to(t => t.Stop());
- first_timer.was_told_to(t => t.Dispose());
- };
-
- it should_start_a_new_timer = () => second_timer.was_told_to(t => t.Start());
-
- context c = () =>
- {
- client = an<ITimerClient>();
- first_timer = dependency<Timer>();
- second_timer = dependency<Timer>();
-
- factory.is_told_to(f => f.create_for(new TimeSpan(0, 1, 1))).it_will_return(first_timer);
- factory.is_told_to(f => f.create_for(new TimeSpan(0, 2, 2))).it_will_return(second_timer);
- };
-
- because b = () =>
- {
- sut.start_notifying(client, new TimeSpan(0, 1, 1));
- sut.start_notifying(client, new TimeSpan(0, 2, 2));
- };
-
- static ITimerClient client;
- static Timer first_timer;
- static Timer second_timer;
- }
-
- [Concern(typeof (IntervalTimer))]
- public class when_a_timer_elapses : behaves_like_an_interval_timer
- {
- it should_notify_the_timer_client = () => client.was_told_to(c => c.notify());
-
- static ITimerClient client;
- static Timer timer;
-
- context c = () =>
- {
- client = an<ITimerClient>();
- timer = dependency<Timer>();
- factory.is_told_to(f => f.create_for(Arg<TimeSpan>.Is.Anything)).it_will_return(timer);
- };
-
- because b = () =>
- {
- sut.start_notifying(client, new TimeSpan(0, 10, 0));
- timer.Raise(t => t.Elapsed += null, timer, null);
- };
- }
-
- [Concern(typeof (IntervalTimer))]
- public class when_stopping_notifications_for_an_existing_timer_client : behaves_like_an_interval_timer
- {
- static ITimerClient client;
- static Timer timer;
-
- it should_stop_the_timer_that_was_started_for_the_client = () => timer.was_told_to(t => t.Stop());
-
- it should_dispose_the_timer_that_was_started_for_the_client = () => timer.was_told_to(t => t.Dispose());
-
- context c = () =>
- {
- client = an<ITimerClient>();
- timer = dependency<Timer>();
-
- when_the(factory).is_told_to(t => t.create_for(Arg<TimeSpan>.Is.Anything)).it_will_return(
- timer);
- };
-
- because b = () =>
- {
- sut.start_notifying(client, new TimeSpan(0, 0, 1));
- sut.stop_notifying(client);
- };
- }
-
- [Concern(typeof (IntervalTimer))]
- public class when_attempting_to_stop_notification_for_a_client_that_doesnt_have_a_timer_started_for_it :
- behaves_like_an_interval_timer
- {
- it should_not_blow_up = () => { };
-
- context c = () => { client = an<ITimerClient>(); };
-
- because b = () => sut.stop_notifying(client);
-
- static ITimerClient client;
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/ITimerClient.cs
@@ -1,7 +0,0 @@
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public interface ITimerClient
- {
- void notify();
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/IWorkerThread.cs
@@ -1,12 +0,0 @@
-using System;
-using System.ComponentModel;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public interface IWorkerThread : IDisposable
- {
- event DoWorkEventHandler DoWork;
- event EventHandler Disposed;
- void begin();
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/SynchronizationContextFactory.cs
@@ -1,25 +0,0 @@
-using System.Threading;
-using Gorilla.Commons.Infrastructure.Container;
-using Gorilla.Commons.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public interface ISynchronizationContextFactory : IFactory<ISynchronizationContext>
- {
- }
-
- public class SynchronizationContextFactory : ISynchronizationContextFactory
- {
- readonly IDependencyRegistry registry;
-
- public SynchronizationContextFactory(IDependencyRegistry registry)
- {
- this.registry = registry;
- }
-
- public ISynchronizationContext create()
- {
- return new SynchronizedContext(registry.get_a<SynchronizationContext>());
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/SynchronizedCommand.cs
@@ -1,30 +0,0 @@
-using System;
-using System.Threading;
-using Gorilla.Commons.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public interface ISynchronizedCommand : IParameterizedCommand<Action>, IParameterizedCommand<ICommand>
- {
- }
-
- public class SynchronizedCommand : ISynchronizedCommand
- {
- readonly SynchronizationContext context;
-
- public SynchronizedCommand(SynchronizationContext context)
- {
- this.context = context;
- }
-
- public void run(Action item)
- {
- context.Post(x => item(), new object());
- }
-
- public void run(ICommand item)
- {
- run(item.run);
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/SynchronizedContext.cs
@@ -1,25 +0,0 @@
-using System.Threading;
-using Gorilla.Commons.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public interface ISynchronizationContext : IParameterizedCommand<ICommand>
- {
- }
-
- public class SynchronizedContext : ISynchronizationContext
- {
- readonly SynchronizationContext context;
-
- public SynchronizedContext(SynchronizationContext context)
- {
- this.context = context;
- }
-
- public void run(ICommand item)
- {
- context.Post(x => item.run(), new object());
- //context.Send(x => item.run(), new object());
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/Synchronizer.cs
@@ -1,189 +0,0 @@
-using System;
-using System.Collections;
-using System.ComponentModel;
-using System.Diagnostics;
-using System.Security.Permissions;
-using System.Threading;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- [SecurityPermission(SecurityAction.Demand, ControlThread = true)]
- public class Synchronizer : ISynchronizeInvoke, IDisposable
- {
- readonly WorkerThread worker_thread;
-
- public Synchronizer()
- {
- worker_thread = new WorkerThread(this);
- }
-
- public bool InvokeRequired
- {
- get { return ReferenceEquals(Thread.CurrentThread, worker_thread); }
- }
-
- public IAsyncResult BeginInvoke(Delegate method, object[] args)
- {
- var result = new WorkItem(null, method, args);
- worker_thread.queue_work_item(result);
- return result;
- }
-
- public object EndInvoke(IAsyncResult result)
- {
- result.AsyncWaitHandle.WaitOne();
- return ((WorkItem) result).MethodReturnedValue;
- }
-
- public object Invoke(Delegate method, object[] args)
- {
- return EndInvoke(BeginInvoke(method, args));
- }
-
- ~Synchronizer()
- {
- }
-
- public void Dispose()
- {
- worker_thread.kill();
- }
-
- class WorkerThread
- {
- Thread thread;
- bool end_loop;
- readonly Mutex end_loop_mutex;
- readonly AutoResetEvent item_added;
- Synchronizer synchronizer;
- readonly Queue work_item_queue;
-
- internal WorkerThread(Synchronizer synchronizer)
- {
- this.synchronizer = synchronizer;
- end_loop = false;
- thread = null;
- end_loop_mutex = new Mutex();
- item_added = new AutoResetEvent(false);
- work_item_queue = new Queue();
- create_thread(true);
- }
-
- internal void queue_work_item(WorkItem work_item)
- {
- lock (work_item_queue.SyncRoot)
- {
- work_item_queue.Enqueue(work_item);
- item_added.Set();
- }
- }
-
- bool EndLoop
- {
- set
- {
- end_loop_mutex.WaitOne();
- end_loop = value;
- end_loop_mutex.ReleaseMutex();
- }
- get
- {
- var result = false;
- end_loop_mutex.WaitOne();
- result = end_loop;
- end_loop_mutex.ReleaseMutex();
- return result;
- }
- }
-
- Thread create_thread(bool auto_start)
- {
- if (thread != null)
- {
- Debug.Assert(false);
- return thread;
- }
- thread = new Thread(run) {Name = "Synchronizer Worker Thread"};
- if (auto_start)
- {
- thread.Start();
- }
- return thread;
- }
-
- void start()
- {
- Debug.Assert(thread != null);
- Debug.Assert(thread.IsAlive == false);
- thread.Start();
- }
-
- bool queue_empty
- {
- get
- {
- lock (work_item_queue.SyncRoot)
- {
- if (work_item_queue.Count > 0)
- {
- return false;
- }
- return true;
- }
- }
- }
-
- WorkItem GetNext()
- {
- if (queue_empty)
- {
- return null;
- }
- lock (work_item_queue.SyncRoot)
- {
- return (WorkItem) work_item_queue.Dequeue();
- }
- }
-
- void run()
- {
- while (EndLoop == false)
- {
- while (queue_empty == false)
- {
- if (EndLoop)
- {
- return;
- }
- var workItem = GetNext();
- workItem.CallBack();
- }
- item_added.WaitOne();
- }
- }
-
- public void kill()
- {
- //Kill is called on client thread - must use cached thread object
- Debug.Assert(thread != null);
- if (thread.IsAlive == false)
- {
- return;
- }
- EndLoop = true;
- item_added.Set();
-
- //Wait for thread to die
- thread.Join();
- if (end_loop_mutex != null)
- {
- end_loop_mutex.Close();
- }
- if (item_added != null)
- {
- item_added.Close();
- }
- }
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/ThreadingExtensions.cs
@@ -1,12 +0,0 @@
-using MoMoney.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public static class ThreadingExtensions
- {
- public static IBackgroundThread on_a_background_thread(this IDisposableCommand command)
- {
- return new BackgroundThread(command);
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/TimerFactory.cs
@@ -1,22 +0,0 @@
-using System;
-using System.Timers;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public interface ITimerFactory
- {
- Timer create_for(TimeSpan span);
- }
-
- public class TimerFactory : ITimerFactory
- {
- public Timer create_for(TimeSpan span)
- {
- if (span.Seconds > 0) {
- var milliseconds = span.Seconds*1000;
- return new Timer(milliseconds);
- }
- return new Timer(span.Ticks);
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/TimerFactorySpecs.cs
@@ -1,43 +0,0 @@
-using System;
-using System.Timers;
-using developwithpassion.bdd.contexts;
-using Gorilla.Commons.Testing;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- [Concern(typeof (TimerFactory))]
- public abstract class behaves_like_a_timer_factory : concerns_for<ITimerFactory, TimerFactory>
- {
- public override ITimerFactory create_sut()
- {
- return new TimerFactory();
- }
- }
-
- [Concern(typeof (TimerFactory))]
- public class when_creating_a_timer : behaves_like_a_timer_factory
- {
- it should_return_a_timer_with_the_correct_interval = () => result.Interval.should_be_equal_to(1000);
-
- because b = () => { result = sut.create_for(new TimeSpan(0, 0, 1)); };
-
- static Timer result;
- }
-
- [Concern(typeof (TimerFactory))]
- public class when_creating_a_timer_with_an_interval_in_milliseconds : behaves_like_a_timer_factory
- {
- it should_return_a_timer_with_the_correct_polling_interval =
- () => result.Interval.should_be_equal_to(milliseconds);
-
- because b = () =>
- {
- var timer_interval = new TimeSpan(50);
- milliseconds = 50;
- result = sut.create_for(timer_interval);
- };
-
- static Timer result;
- static double milliseconds;
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/WorkerThread.cs
@@ -1,50 +0,0 @@
-using System;
-using System.ComponentModel;
-using Gorilla.Commons.Infrastructure.Logging;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- public class WorkerThread : Component, IWorkerThread
- {
- static readonly object do_work_key = new object();
- bool is_running;
- readonly Action background_thread;
-
- public WorkerThread()
- {
- background_thread = worker_thread_start;
- }
-
- public event DoWorkEventHandler DoWork
- {
- add { Events.AddHandler(do_work_key, value); }
- remove { Events.RemoveHandler(do_work_key, value); }
- }
-
- public void begin()
- {
- if (is_running)
- {
- throw new InvalidOperationException("Worker Thread Is Already Running");
- }
- is_running = true;
- background_thread.BeginInvoke(null, null);
- }
-
- void worker_thread_start()
- {
- try
- {
- var handler = (DoWorkEventHandler) Events[do_work_key];
- if (handler != null)
- {
- handler(this, new DoWorkEventArgs(null));
- }
- }
- catch (Exception e)
- {
- this.log().error(e);
- }
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Threading/WorkItem.cs
@@ -1,93 +0,0 @@
-using System;
-using System.Threading;
-
-namespace Gorilla.Commons.Infrastructure.Threading
-{
- [Serializable]
- internal class WorkItem : IAsyncResult
- {
- readonly object[] args;
- readonly object async_state;
- bool completed;
- readonly Delegate method;
- readonly ManualResetEvent reset_event;
- object returned_value;
-
- internal WorkItem(object async_state, Delegate method, object[] args)
- {
- this.async_state = async_state;
- this.method = method;
- this.args = args;
- reset_event = new ManualResetEvent(false);
- completed = false;
- }
-
- //IAsyncResult properties
- object IAsyncResult.AsyncState
- {
- get { return async_state; }
- }
-
- WaitHandle IAsyncResult.AsyncWaitHandle
- {
- get { return reset_event; }
- }
-
- bool IAsyncResult.CompletedSynchronously
- {
- get { return false; }
- }
-
- bool IAsyncResult.IsCompleted
- {
- get { return Completed; }
- }
-
- bool Completed
- {
- get
- {
- lock (this)
- {
- return completed;
- }
- }
- set
- {
- lock (this)
- {
- completed = value;
- }
- }
- }
-
- //This method is called on the worker thread to execute the method
- internal void CallBack()
- {
- MethodReturnedValue = method.DynamicInvoke(args);
- //Method is done. Signal the world
- reset_event.Set();
- Completed = true;
- }
-
- internal object MethodReturnedValue
- {
- get
- {
- object method_returned_value;
- lock (this)
- {
- method_returned_value = returned_value;
- }
- return method_returned_value;
- }
- set
- {
- lock (this)
- {
- returned_value = value;
- }
- }
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure/Gorilla.Commons.Infrastructure.csproj
@@ -71,10 +71,6 @@
<Compile Include="Container\Resolve.cs" />
<Compile Include="Container\ResolveSpecs.cs" />
<Compile Include="Debugging\Launch.cs" />
- <Compile Include="Eventing\EventAggregator.cs" />
- <Compile Include="Eventing\EventAggregatorSpecs.cs" />
- <Compile Include="Eventing\IEvent.cs" />
- <Compile Include="Eventing\IEventSubscriber.cs" />
<Compile Include="FileSystem\ApplicationFile.cs" />
<Compile Include="FileSystem\IFile.cs" />
<Compile Include="Logging\TextLogger.cs" />
@@ -96,29 +92,6 @@
<Compile Include="Reflection\IAssembly.cs" />
<Compile Include="Registries\DefaultRegistry.cs" />
<Compile Include="Registries\DefaultRegistrySpecs.cs" />
- <Compile Include="Threading\AsynchronousCommandProcessor.cs" />
- <Compile Include="Threading\BackgroundThread.cs" />
- <Compile Include="Threading\BackgroundThreadFactory.cs" />
- <Compile Include="Threading\BackgroundThreadFactorySpecs.cs" />
- <Compile Include="Threading\BackgroundThreadSpecs.cs" />
- <Compile Include="Threading\CommandProcessor.cs" />
- <Compile Include="Threading\CommandProcessorSpecs.cs" />
- <Compile Include="Threading\ICommandProcessor.cs" />
- <Compile Include="Threading\IntervalTimer.cs" />
- <Compile Include="Threading\IntervalTimerSpecs.cs" />
- <Compile Include="Threading\ITimerClient.cs" />
- <Compile Include="Threading\IWorkerThread.cs" />
- <Compile Include="Threading\SynchronizationContextFactory.cs" />
- <Compile Include="Threading\SynchronizedCommand.cs" />
- <Compile Include="Threading\SynchronizedContext.cs" />
- <Compile Include="Threading\Synchronizer.cs" />
- <Compile Include="Threading\ThreadingExtensions.cs" />
- <Compile Include="Threading\TimerFactory.cs" />
- <Compile Include="Threading\TimerFactorySpecs.cs" />
- <Compile Include="Threading\WorkerThread.cs">
- <SubType>Component</SubType>
- </Compile>
- <Compile Include="Threading\WorkItem.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Gorilla.Commons.Testing\Gorilla.Commons.Testing.csproj">
product/Gorilla.Commons.Infrastructure.ThirdParty/Castle/DynamicProxy/Interceptors/RaiseEventInterceptor.cs
@@ -1,25 +0,0 @@
-using Castle.Core.Interceptor;
-using Gorilla.Commons.Infrastructure.Eventing;
-
-namespace Gorilla.Commons.Infrastructure.Castle.DynamicProxy.Interceptors
-{
- public interface IRaiseEventInterceptor<Event> : IInterceptor where Event : IEvent, new()
- {
- }
-
- public class RaiseEventInterceptor<Event> : IRaiseEventInterceptor<Event> where Event : IEvent, new()
- {
- readonly IEventAggregator broker;
-
- public RaiseEventInterceptor(IEventAggregator broker)
- {
- this.broker = broker;
- }
-
- public void Intercept(IInvocation invocation)
- {
- invocation.Proceed();
- broker.publish(new Event());
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure.ThirdParty/Castle/DynamicProxy/Interceptors/RunOnBackgroundThreadInterceptor.cs
@@ -1,25 +0,0 @@
-using Castle.Core.Interceptor;
-using Gorilla.Commons.Infrastructure.Threading;
-using MoMoney.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Castle.DynamicProxy.Interceptors
-{
- public class RunOnBackgroundThreadInterceptor<CommandToExecute> : IInterceptor
- where CommandToExecute : IDisposableCommand
- {
- readonly IBackgroundThreadFactory thread_factory;
-
- public RunOnBackgroundThreadInterceptor(IBackgroundThreadFactory thread_factory)
- {
- this.thread_factory = thread_factory;
- }
-
- public virtual void Intercept(IInvocation invocation)
- {
- using (thread_factory.create_for<CommandToExecute>())
- {
- invocation.Proceed();
- }
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure.ThirdParty/Castle/DynamicProxy/Interceptors/RunOnBackgroundThreadInterceptorSpecs.cs
@@ -1,45 +0,0 @@
-using Castle.Core.Interceptor;
-using developwithpassion.bdd.contexts;
-using Gorilla.Commons.Infrastructure.Threading;
-using Gorilla.Commons.Testing;
-using MoMoney.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Castle.DynamicProxy.Interceptors
-{
- [Concern(typeof (RunOnBackgroundThreadInterceptor<>))]
- public abstract class behaves_like_background_thread_interceptor :
- concerns_for<IInterceptor, RunOnBackgroundThreadInterceptor<IDisposableCommand>>
- {
- context c = () => { thread_factory = the_dependency<IBackgroundThreadFactory>(); };
-
- static protected IBackgroundThreadFactory thread_factory;
- }
-
- [Concern(typeof (RunOnBackgroundThreadInterceptor<>))]
- public class when_intercepting_a_call_to_a_method_that_takes_a_long_time_to_complete :
- behaves_like_background_thread_interceptor
- {
- context c = () =>
- {
- invocation = an<IInvocation>();
- background_thread = an<IBackgroundThread>();
- thread_factory
- .is_told_to(f => f.create_for<IDisposableCommand>())
- .it_will_return(background_thread);
- };
-
- because b = () => sut.Intercept(invocation);
-
- it should_display_a_progress_bar_on_a_background_thread =
- () => thread_factory.was_told_to(f => f.create_for<IDisposableCommand>());
-
- it should_proceed_with_the_orginal_invocation_on_the_actual_object =
- () => invocation.was_told_to(i => i.Proceed());
-
- it should_hide_the_progress_bar_when_the_invocation_is_completed =
- () => background_thread.was_told_to(b => b.Dispose());
-
- static IInvocation invocation;
- static IBackgroundThread background_thread;
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure.ThirdParty/Castle/DynamicProxy/Interceptors/RunOnUIThread.cs
@@ -1,25 +0,0 @@
-using Castle.Core.Interceptor;
-using Gorilla.Commons.Infrastructure.Threading;
-using Gorilla.Commons.Utility.Core;
-
-namespace Gorilla.Commons.Infrastructure.Castle.DynamicProxy.Interceptors
-{
- public class RunOnUIThread : IInterceptor
- {
- readonly ISynchronizationContextFactory factory;
-
- public RunOnUIThread() : this(Lazy.load<ISynchronizationContextFactory>())
- {
- }
-
- public RunOnUIThread(ISynchronizationContextFactory factory)
- {
- this.factory = factory;
- }
-
- public void Intercept(IInvocation invocation)
- {
- factory.create().run(new ActionCommand(invocation.Proceed));
- }
- }
-}
\ No newline at end of file
product/Gorilla.Commons.Infrastructure.ThirdParty/Gorilla.Commons.Infrastructure.ThirdParty.csproj
@@ -92,8 +92,6 @@
<Compile Include="Autofac\AutofacDependencyRegistry.cs" />
<Compile Include="Autofac\AutofacDependencyRegistryBuilder.cs" />
<Compile Include="Autofac\AutofacSpecs.cs" />
- <Compile Include="Castle\DynamicProxy\Interceptors\RaiseEventInterceptor.cs" />
- <Compile Include="Castle\DynamicProxy\Interceptors\RunOnUIThread.cs" />
<Compile Include="Castle\DynamicProxy\Interceptors\SynchronizedInterceptor.cs" />
<Compile Include="Castle\Windsor\WindsorExtensions.cs" />
<Compile Include="IDependencyRegistration.cs" />
@@ -108,8 +106,6 @@
<Compile Include="Castle\DynamicProxy\Interceptors\IMethodCallTracker.cs" />
<Compile Include="Castle\DynamicProxy\Interceptors\MethodCallTracker.cs" />
<Compile Include="Castle\DynamicProxy\Interceptors\MethodCallTrackerSpecs.cs" />
- <Compile Include="Castle\DynamicProxy\Interceptors\RunOnBackgroundThreadInterceptor.cs" />
- <Compile Include="Castle\DynamicProxy\Interceptors\RunOnBackgroundThreadInterceptorSpecs.cs" />
<Compile Include="Castle\DynamicProxy\Interceptors\SelectiveInterceptor.cs" />
<Compile Include="Castle\DynamicProxy\IProxyBuilder.cs" />
<Compile Include="Castle\DynamicProxy\IProxyFactory.cs" />