Ping pong Bruce Lee test

There is a famous Bruce Lee clip showing him as a very good ping pong player using unusual tooling to get the job done. It thought that this ping pong match would be a great story for writing a test for my RampUp library, especially when I provided the first, most likely not final, version of the actor system.

To have more fun I split Bruce Lee into Bruce & Lee. Each part of Bruce Lee either pings or pongs.


public class Bruce : IHandle<Ping>
{
    public IBus Bus;

    public void Handle(ref Envelope envelope, ref Ping msg)
    {
        var p = new Pong();
        Bus.Publish(ref p);
    }
}

public class Lee : IHandle<Pong>
{
    public IBus Bus;

    public void Handle(ref Envelope envelope, ref Pong msg)
    {
        var p = new Ping();
        Bus.Publish(ref p);
    }
}

The ping/pong messages are only markups:


public struct Pong : IMessage {}

public struct Ping : IMessage {}

And the final execution of this setup can be summarized in:


public class Program
{
    public static void Main()
    {
        var system = new ActorSystem();
        IBus bus = null;

        system.Add(new Bruce(), ctx => { bus = ctx.Actor.Bus = ctx.Bus; });
        system.Add(new Lee(), ctx => { ctx.Actor.Bus = ctx.Bus; });

        system.Start();

        var p = new Pong();
        bus.Publish(ref p); // pong as Bruce
        // ... later
        system.Stop();
    }
}

I hope you like the example. I’m aware that ActorSystem API isn’t the best possible API ever, but even in this shape enables me to push RampUp forward.

2 thoughts on “Ping pong Bruce Lee test

  1. I see that all those IHandle/IBus/Publish concepts has evolved into something sweet. The one thing I can’t get is this IBus assignment – why are you getting an instance from Bruce instead of Lee? Can’t you get it from ActorSystem?

    • I’m glad you like the higher level API. For now there’s no bus that can be obtained from the system itself, but I’ll add it for sure. I desperately needed to provide any kind of API to enable me and potential community members to shape the rest of RampUp.

Comments are closed.