Last active
December 9, 2015 21:05
-
-
Save Horusiath/add32b8ce8dc3c56dc44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class GetState { } | |
public sealed class Increment { } | |
public sealed class Changed { } | |
public class TestCaseActor : UntypedPersistentActor | |
{ | |
private int state = 0; | |
public override string PersistenceId { get; } = "yolo"; | |
protected override void OnCommand(object message) | |
{ | |
Active(message); | |
} | |
private void Active(object message) | |
{ | |
message.Match() | |
.With<Increment>(inc => | |
{ | |
if (state >= 3) | |
Context.Become(Freezed); | |
else | |
Persist(new Changed(), UpdateState); | |
}) | |
.With<GetState>(_ => Sender.Tell(state)); | |
} | |
private void Freezed(object message) | |
{ | |
message.Match() | |
.With<GetState>(_ => Sender.Tell(state)); | |
} | |
protected override void OnRecover(object message) | |
{ | |
message.Match() | |
.With<Changed>(UpdateState); | |
} | |
private void UpdateState(Changed e) | |
{ | |
state ++; | |
} | |
} | |
// exec | |
using (var system = ActorSystem.Create("failing-sys")) | |
{ | |
var pref = system.ActorOf(Props.Create<TestCaseActor>()); | |
pref.Tell(new Increment()); | |
pref.Tell(new Increment()); | |
pref.Tell(new Increment()); | |
pref.Tell(new Increment()); | |
pref.Tell(new Increment()); | |
var state = pref.Ask<int>(new GetState()).Result; | |
Console.WriteLine("State is: {0}", state); | |
Console.ReadLine(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment