Skip to content

Commit

Permalink
Adding DebugTestHost to support replaying an instance from history
Browse files Browse the repository at this point in the history
  • Loading branch information
samarabbas committed Apr 9, 2015
1 parent 854eea9 commit 5826ec6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions Framework/Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<Compile Include="OrchestrationState.cs" />
<Compile Include="TaskActivityDispatcherSettings.cs" />
<Compile Include="TaskHubClientSettings.cs" />
<Compile Include="Test\DebugTestHost.cs" />
<Compile Include="TrackingDispatcherSettings.cs" />
<Compile Include="TaskOrchestrationDispatcherSettings.cs" />
<Compile Include="TaskHubWorkerSettings.cs" />
Expand Down
89 changes: 89 additions & 0 deletions Framework/Test/DebugTestHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace DurableTask.Test
{
using System;
using System.Collections.Generic;
using System.Linq;
using DurableTask.History;
using Newtonsoft.Json;

public class DebugTestHost
{
NameVersionObjectManager<TaskOrchestration> orchestrationObjectManager;

public DebugTestHost()
{
this.orchestrationObjectManager = new NameVersionObjectManager<TaskOrchestration>();
}

public DebugTestHost AddTaskOrchestrations(params Type[] taskOrchestrationTypes)
{
foreach (Type type in taskOrchestrationTypes)
{
ObjectCreator<TaskOrchestration> creator = new DefaultObjectCreator<TaskOrchestration>(type);
this.orchestrationObjectManager.Add(creator);
}

return this;
}

public DebugTestHost AddTaskOrchestrations(params ObjectCreator<TaskOrchestration>[] taskOrchestrationCreators)
{
foreach (ObjectCreator<TaskOrchestration> creator in taskOrchestrationCreators)
{
this.orchestrationObjectManager.Add(creator);
}

return this;
}

public string ReplayOrchestration(Type orchestrationType, string serializedHistoryEvents)
{
return ReplayOrchestration(
NameVersionHelper.GetDefaultName(orchestrationType),
NameVersionHelper.GetDefaultVersion(orchestrationType),
serializedHistoryEvents);
}

public string ReplayOrchestration(string name, string version, string serializedHistoryEvents)
{

TaskOrchestration taskOrchestration = this.orchestrationObjectManager.GetObject(name, version);

IList<HistoryEvent> replayEvents = this.DeserializeHistoryEvents(serializedHistoryEvents);

if (replayEvents.Any(re => re.EventType == EventType.GenericEvent))
{
throw new InvalidOperationException("Cannot replay with GenericEvent");
}

var runtimeState = new OrchestrationRuntimeState(this.DeserializeHistoryEvents(serializedHistoryEvents));

TaskOrchestrationExecutor executor = new TaskOrchestrationExecutor(runtimeState, taskOrchestration);
return JsonConvert.SerializeObject(executor.Execute(), new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.All,
Formatting = Formatting.Indented
});
}

IList<HistoryEvent> DeserializeHistoryEvents(string serializedHistoryEvents)
{
return JsonConvert.DeserializeObject<IList<HistoryEvent>>(
serializedHistoryEvents,
new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
}
}
}

0 comments on commit 5826ec6

Please sign in to comment.