Skip to content

Commit

Permalink
ported root level classes
Browse files Browse the repository at this point in the history
  • Loading branch information
zpbappi committed Nov 27, 2018
1 parent f181a36 commit ce0e70a
Show file tree
Hide file tree
Showing 71 changed files with 1,089 additions and 1,174 deletions.
2 changes: 1 addition & 1 deletion src/Vlingo.Actors.Tests/ActorEnvironmentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void TestExpectedEnvironment()
var actorDefinition = state.ValueOf<Definition>("definition");

Assert.Empty(TestWorld.AllMessagesFor(env.Address));
Assert.Equal(TestWorld.World.AddressFactory.TestNextIdValue() - 1, state.ValueOf<Address>("address").Id);
Assert.Equal(TestWorld.World.AddressFactory.TestNextIdValue() - 1, state.ValueOf<IAddress>("address").Id);
Assert.Equal(definition.ActorName, actorDefinition.ActorName);
Assert.Equal(definition.Parameters(), actorDefinition.Parameters());
Assert.Equal(TestWorld.World.DefaultParent, state.ValueOf<Actor>("parent"));
Expand Down
4 changes: 2 additions & 2 deletions src/Vlingo.Actors.Tests/ActorLifecycleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public LifecycleActor(TestResults testResults)
this.testResults = testResults;
}

internal protected override void BeforeStart()
protected internal override void BeforeStart()
{
testResults.receivedBeforeStart.Set(true);
testResults.until.Happened();
}

internal protected override void AfterStop()
protected internal override void AfterStop()
{
testResults.receivedAfterStop.Set(true);
testResults.until.Happened();
Expand Down
4 changes: 2 additions & 2 deletions src/Vlingo.Actors.Tests/ActorStopTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void CreateChildren()
ChildActorFor<IChildCreatingStoppable>(Definition.Has<ChildCreatingStoppableActor>(Definition.Parameters(testResults), $"{pre}.3"));
}

internal protected override void BeforeStart()
protected internal override void BeforeStart()
{
base.BeforeStart();
if (testResults.untilStart != null)
Expand All @@ -105,7 +105,7 @@ internal protected override void BeforeStart()
}

private static readonly object afterStopMutex = new object();
internal protected override void AfterStop()
protected internal override void AfterStop()
{
lock (afterStopMutex)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Vlingo.Actors.Tests/Supervision/FailureControlActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,35 @@ public void FailNow()
throw new ApplicationException("Intended failure.");
}

internal protected override void BeforeStart()
protected internal override void BeforeStart()
{
testResults.BeforeStartCount.IncrementAndGet();
testResults.UntilFailNow.Happened();
base.BeforeStart();
}

internal protected override void AfterStop()
protected internal override void AfterStop()
{
testResults.AfterStopCount.IncrementAndGet();
testResults.UntilFailNow.Happened();
base.AfterStop();
}

internal protected override void BeforeRestart(Exception reason)
protected internal override void BeforeRestart(Exception reason)
{
testResults.BeforeRestartCount.IncrementAndGet();
testResults.UntilFailNow.Happened();
base.BeforeRestart(reason);
}

internal protected override void AfterRestart(Exception reason)
protected internal override void AfterRestart(Exception reason)
{
base.AfterRestart(reason);
testResults.AfterRestartCount.IncrementAndGet();
testResults.UntilAfterRestart.Happened();
}

internal protected override void BeforeResume(Exception reason)
protected internal override void BeforeResume(Exception reason)
{
testResults.BeforeResume.IncrementAndGet();
testResults.UntilBeforeResume.Happened();
Expand Down
66 changes: 32 additions & 34 deletions src/Vlingo.Actors/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

using System;
using Vlingo.Actors.TestKit;
using Vlingo.Common;

namespace Vlingo.Actors
{
public abstract class Actor : IStartable, IStoppable, ITestStateView
{
internal ICompletes completes;
internal ResultCompletes completes;
internal LifeCycle LifeCycle { get; }

public virtual Address Address => LifeCycle.Address;
public virtual IAddress Address => LifeCycle.Address;

public virtual IDeadLetters DeadLetters => LifeCycle.Environment.Stage.World.DeadLetters;

Expand All @@ -33,6 +34,7 @@ public virtual void Stop()
{
if (LifeCycle.Address.Id != World.DeadLettersId)
{
// TODO: remove this actor as a child on parent
LifeCycle.Stop(this);
}
}
Expand All @@ -54,7 +56,7 @@ public override bool Equals(object other)

public override string ToString() => $"Actor[type={GetType().Name} address={Address}]";

internal Actor Parent
internal virtual Actor Parent
{
get
{
Expand All @@ -72,9 +74,10 @@ protected Actor()
var maybeEnvironment = ActorFactory.ThreadLocalEnvironment.Value;
LifeCycle = new LifeCycle(maybeEnvironment ?? new TestEnvironment());
ActorFactory.ThreadLocalEnvironment.Value = null;
completes = new ResultCompletes();
}

protected T ChildActorFor<T>(Definition definition)
protected internal virtual T ChildActorFor<T>(Definition definition)
{
if (definition.Supervisor != null)
{
Expand All @@ -98,7 +101,7 @@ protected T ChildActorFor<T>(Definition definition)
}
}

internal protected ICompletes<T> Completes<T>()
protected internal virtual ICompletes<T> Completes<T>()
{
if(completes == null)
{
Expand All @@ -108,11 +111,14 @@ internal protected ICompletes<T> Completes<T>()
return (ICompletes<T>)completes;
}

protected Definition Definition => LifeCycle.Definition;
protected internal virtual ICompletesEventually CompletesEventually()
=> LifeCycle.Environment.Stage.World.CompletesFor(completes.ClientCompletes());

internal protected ILogger Logger => LifeCycle.Environment.Logger;
protected internal virtual Definition Definition => LifeCycle.Definition;

protected T ParentAs<T>()
protected internal virtual ILogger Logger => LifeCycle.Environment.Logger;

protected internal virtual T ParentAs<T>()
{
if (LifeCycle.Environment.IsSecured)
{
Expand All @@ -123,26 +129,17 @@ protected T ParentAs<T>()
return LifeCycle.Environment.Stage.ActorProxyFor<T>(parent, parent.LifeCycle.Environment.Mailbox);
}

protected void Secure()
protected virtual void Secure()
{
LifeCycle.Secure();
}

internal protected T SelfAs<T>()
protected internal virtual T SelfAs<T>()
{
return LifeCycle.Environment.Stage.ActorProxyFor<T>(this, LifeCycle.Environment.Mailbox);
}

protected IOutcomeInterest<TOutcome> SelfAsOutcomeInterest<TOutcome, TRef>(TRef reference)
{
var outcomeAware = LifeCycle.Environment.Stage.ActorProxyFor<IOutcomeAware<TOutcome, TRef>>(
this,
LifeCycle.Environment.Mailbox);

return new OutcomeInterestActorProxy<TOutcome, TRef>(outcomeAware, reference);
}

internal protected Stage Stage
protected internal Stage Stage
{
get
{
Expand All @@ -155,7 +152,7 @@ internal protected Stage Stage
}
}

internal protected Stage StageNamed(string name)
protected internal virtual Stage StageNamed(string name)
{
return LifeCycle.Environment.Stage.World.StageNamed(name);
}
Expand All @@ -164,51 +161,52 @@ internal protected Stage StageNamed(string name)
// stowing/dispersing
//=======================================

internal protected virtual bool IsDispersing =>
LifeCycle.Environment.Stowage.IsDispersing;
protected internal virtual bool IsDispersing =>
LifeCycle.IsDispersing;


internal protected virtual void DisperseStowedMessages()
protected internal virtual void DisperseStowedMessages()
{
LifeCycle.Environment.Stowage.DispersingMode();
LifeCycle.DisperseStowedMessages();
}

internal protected virtual bool IsStowing =>
LifeCycle.Environment.Stowage.IsStowing;
protected internal virtual bool IsStowing =>
LifeCycle.IsStowing;


internal protected virtual void StowMessages()
protected internal virtual void StowMessages(params Type[] stowageOverrides)
{
LifeCycle.Environment.Stowage.StowingMode();
LifeCycle.StowMessages();
LifeCycle.Environment.StowageOverrides(stowageOverrides);
}

//=======================================
// life cycle overrides
//=======================================

internal protected virtual void BeforeStart()
protected internal virtual void BeforeStart()
{
// override
}

internal protected virtual void AfterStop()
protected internal virtual void AfterStop()
{
// override
}

internal protected virtual void BeforeRestart(Exception reason)
protected internal virtual void BeforeRestart(Exception reason)
{
// override
LifeCycle.AfterStop(this);
}

internal protected virtual void AfterRestart(Exception reason)
protected internal virtual void AfterRestart(Exception reason)
{
// override
LifeCycle.BeforeStart(this);
}

internal protected virtual void BeforeResume(Exception reason)
protected internal virtual void BeforeResume(Exception reason)
{
// override
}
Expand Down
64 changes: 58 additions & 6 deletions src/Vlingo.Actors/ActorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,59 @@

namespace Vlingo.Actors
{
internal class ActorFactory
internal static class ActorFactory
{
internal static readonly ThreadLocal<Environment> ThreadLocalEnvironment = new ThreadLocal<Environment>(false);

public static Type ActorClassWithProtocol<TProtocol>(string actorClassname) where TProtocol : Actor
=> ActorClassWithProtocol(actorClassname, typeof(TProtocol));

public static Type ActorClassWithProtocol(string actorClassname, Type protocolClass)
{
try
{
var actorClass = Type.GetType(actorClassname);
AssertActorWithProtocol(actorClass, protocolClass);
return actorClass;
}
catch (Exception e)
{
throw new ArgumentException($"The class {actorClassname} cannot be loaded because: {e.Message}", e);
}
}

private static void AssertActorWithProtocol(Type candidateActorClass, Type protocolClass)
{
var superclass = candidateActorClass.BaseType;
while (superclass != null)
{
if (superclass == typeof(Actor))
{
break;
}
superclass = superclass.BaseType;
}

if (superclass == null)
{
throw new ArgumentException($"Class must extend Vlingo.Actors.Actor: {candidateActorClass.FullName}");
}

foreach (var protocolInterfaceClass in candidateActorClass.GetInterfaces())
{
if (protocolClass == protocolInterfaceClass)
{
return;
}
}
throw new ArgumentException($"Actor class {candidateActorClass.FullName} must implement: {protocolClass.FullName}");
}

internal static Actor ActorFor(
Stage stage,
Actor parent,
Definition definition,
Address address,
IAddress address,
IMailbox mailbox,
ISupervisor supervisor,
ILogger logger)
Expand Down Expand Up @@ -54,10 +98,18 @@ internal static Actor ActorFor(
actor = (Actor)ctor.Invoke(definition.InternalParameters().ToArray());
actor.LifeCycle.SendStart(actor);
}
catch(Exception ex)
catch (Exception ex)
{
logger.Log($"vlingo-net/actors: ActorFactory: failed because: {ex.Message}", ex);
Console.WriteLine(ex.StackTrace);
var cause = ex.InnerException ?? ex;
logger.Log("ActorFactory: failed actor creation. "
+ "This is sometimes cause be the constructor parameter types not matching "
+ "the types in the Definition.parameters(). Often it is caused by a "
+ "failure in the actor constructor. We have attempted to uncover "
+ "the root cause here, but that may not be available in some cases.\n"
+ "The root cause may be: " + cause.Message + "\n"
+ "See stacktrace for more information. We strongly recommend reviewing your "
+ "constructor for possible failures in dependencies that it creates.",
cause);
}
break;
}
Expand All @@ -78,7 +130,7 @@ internal static Actor ActorFor(

internal static IMailbox ActorMailbox(
Stage stage,
Address address,
IAddress address,
Definition definition)
{
var mailboxName = stage.World.MailboxNameFrom(definition.MailboxName);
Expand Down
Loading

0 comments on commit ce0e70a

Please sign in to comment.