This is an example on how to use GraphWalker’s Websocket in order to run tests written in C#.

Pre-requisites

Get the C# example source code

git clone https://github.com/GraphWalker/graphwalker-example.git
cd graphwalker-example/c-sharp-websocket/SmallModel

Run the test

In a terminal, start GraphWalker as a Websocket service

java -jar graphwalker-cli-3.4.2.jar -d all online

In the folder of graphwalker-example/c-sharp-websocket/SmallModel build the C# project, on linux:

xbuild

Then run the test, on linux:

mono SmallModel/bin/Debug/SmallModel.exe
Will connect to GraphWalker server...                                                                                                   
Connected!                                                                                                                              
Receiving message: {"success":true,"command":"start"}                                                                                   
GraphWalker machine started ok                                                                                                          
Receiving message: {"success":true,"hasNext":true,"command":"hasNext"}                                                                  
hasNext returned true
:
:
:

What happened

SmallModel

When executed, the SmallModel program will connect to the GraphWalker service, load and start running a model in JSON notation.

The GraphWalker Websocket service will get messages from Program.cs, and query the service for steps to execute. The steps are implemented in C# code in SmallModel.cs

The psuedo code would look something like the:

while hasNext()
  step = getNext()
  "Call method step in C# SmallModel class"
  invoke SmallModel.'step'
  print getData()

The actual C# code that queries the Websocket service:

public void run ()
{
  // Create the thread object, passing in the
  // GraphWalkerClientWorker.connect method via a ThreadStart delegate.
  // This does not start the thread.
  GraphWalkerClientWorker worker = new GraphWalkerClientWorker ();
  Thread workerThread = new Thread (worker.connect);

  // Start the thread
  workerThread.Start ();

  // Spin for a while waiting for the started thread to become
  // alive:
  while (!workerThread.IsAlive)
    ;

  worker.connectedEvent.WaitOne ();

  worker.start (model);
  worker.startEvent.WaitOne ();

  Type smallModelType = typeof(SmallModel);
  ConstructorInfo ctor = smallModelType.GetConstructor(System.Type.EmptyTypes);

  while (true) {
    worker.hasNext ();
    worker.hasNextEvent.WaitOne ();
    if (!worker.isHasNext)
      break;

    worker.getNext ();
    worker.getNextEvent.WaitOne ();
    string methodName = (string)worker.getMessage();

    object instance = ctor.Invoke(null);
    MethodInfo methodInfo = smallModelType.GetMethod(methodName);
    methodInfo.Invoke(instance, new object[]{});

    worker.getData ();
    worker.getDataEvent.WaitOne ();
    Console.WriteLine("Data: " + worker.getDataObject().ToString());
  }
  worker.disconnect();
  workerThread.Join ();
}
Tags: example