Skip to content

Commit dc3cb07

Browse files
committed
Add batched commands and sync driver, refine lambda commands
1 parent f3f1e77 commit dc3cb07

File tree

15 files changed

+209
-118
lines changed

15 files changed

+209
-118
lines changed

src/InEngine.Core.Test/Queuing/Commands/ConsumeTest.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,5 @@ public void Setup()
1515
{
1616
InEngineSettings.BasePath = TestContext.CurrentContext.TestDirectory;
1717
}
18-
19-
[Test]
20-
public void ShouldConsumePrimaryQueue()
21-
{
22-
new Publish()
23-
{
24-
Command = new AlwaysSucceed()
25-
}.Run();
26-
27-
Subject.Run();
28-
}
29-
30-
[Test]
31-
public void ShouldConsumeSecondaryQueue()
32-
{
33-
new Publish()
34-
{
35-
Command = new AlwaysSucceed()
36-
}.Run();
37-
Subject.UseSecondaryQueue = true;
38-
39-
Subject.Run();
40-
}
41-
42-
[Test]
43-
public void ShouldConsumeSecondaryQueueBasedOnJobContextData()
44-
{
45-
new Publish()
46-
{
47-
Command = new AlwaysSucceed()
48-
}.Run();
49-
var jobExecutionConext = new Mock<IJobExecutionContext>();
50-
var jobDataMap = new JobDataMap { { "useSecondaryQueue", true } };
51-
jobExecutionConext.SetupGet(p => p.JobDetail.JobDataMap).Returns(jobDataMap);
52-
53-
Subject.Run();
54-
}
5518
}
5619
}

src/InEngine.Core.Test/Queuing/Commands/PublishTest.cs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,5 @@ public void Setup()
1717
{
1818
InEngineSettings.BasePath = TestContext.CurrentContext.TestDirectory;
1919
}
20-
21-
[Test]
22-
public void ShouldPublishCommandObject()
23-
{
24-
Subject.Command = new AlwaysSucceed();
25-
26-
Subject.Run();
27-
}
28-
29-
[Test]
30-
public void ShouldPublishCommandByArgs()
31-
{
32-
var nullCommand = new AlwaysSucceed();
33-
Subject.CommandPlugin = nullCommand.GetType().Assembly.GetName().Name + ".dll";
34-
Subject.CommandClass = nullCommand.GetType().FullName;
35-
36-
Subject.Run();
37-
}
38-
39-
[Test]
40-
public void ShouldPublishManyCommands()
41-
{
42-
43-
foreach (var i in Enumerable.Range(0, 200).ToList())
44-
{
45-
Subject.Command = new Echo()
46-
{
47-
VerbatimText = $"test job: {i}"
48-
};
49-
Subject.Run();
50-
}
51-
}
52-
53-
[Test]
54-
public void ShouldFailWhenCommandDoesNotExist()
55-
{
56-
Subject.CommandPlugin = "foo";
57-
Subject.CommandClass = "bar";
58-
var expectedMessage = "Plugin not found at ";
59-
60-
var result = Assert.Throws<PluginNotFoundException>(() => { Subject.Run(); });
61-
62-
Assert.IsTrue(result.Message.StartsWith(expectedMessage, StringComparison.InvariantCulture));
63-
}
6420
}
6521
}

src/InEngine.Core.Test/Queuing/QueueTest.cs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using InEngine.Commands;
44
using InEngine.Core.Commands;
55
using InEngine.Core.Queuing;
6+
using InEngine.Core.Queuing.Clients;
67
using InEngine.Core.Queuing.Commands;
78
using Moq;
89
using NUnit.Framework;
@@ -11,21 +12,55 @@
1112
namespace InEngine.Core.Test.Queuing
1213
{
1314
[TestFixture]
14-
public class QueueTest : TestBase
15+
public class QueueTest : TestBase<Queue>
1516
{
16-
public Queue Subject { get; private set; }
17+
public Mock<IQueueClient> MockQueueClient { get; set; }
1718

1819
[SetUp]
1920
public void Setup()
2021
{
2122
InEngineSettings.BasePath = TestContext.CurrentContext.TestDirectory;
22-
Subject = Queue.Make();
23+
MockQueueClient = new Mock<IQueueClient>();
24+
Subject.QueueClient = MockQueueClient.Object;
2325
}
2426

2527
[Test]
26-
public void ShouldPublishLambda()
28+
public void ShouldPublishCommand()
2729
{
28-
Subject.Publish(() => { Console.Write("Hello, world."); });
30+
var command = Fake.It();
31+
MockQueueClient.Setup(x => x.Publish(command));
32+
33+
Subject.Publish(command);
34+
35+
MockQueueClient.Verify(x => x.Publish(command), Times.Once());
36+
}
37+
38+
[Test]
39+
public void ShouldPublishLambdaCommand()
40+
{
41+
Action action = () => { Console.Write("Hello, world."); };
42+
var lambda = new Lambda() { Action = action };
43+
MockQueueClient.Setup(x => x.Publish(It.IsAny<Lambda>()));
44+
45+
Subject.Publish(action);
46+
47+
MockQueueClient.Verify(x => x.Publish(It.Is<Lambda>(y => y.Action == action)), Times.Once());
48+
}
49+
50+
[Test]
51+
public void ShouldPublishChainOfCommands()
52+
{
53+
var commands = new[] {
54+
new AlwaysSucceed(),
55+
new AlwaysSucceed(),
56+
new AlwaysSucceed(),
57+
new AlwaysSucceed(),
58+
};
59+
MockQueueClient.Setup(x => x.Publish(It.IsAny<Chain>()));
60+
61+
Subject.Publish(commands);
62+
63+
MockQueueClient.Verify(x => x.Publish(It.Is<Chain>(y => y.Commands.Equals(commands))), Times.Once());
2964
}
3065
}
3166
}

src/InEngine.Core.Test/Scheduling/ScheduleTest.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
using System;
22
using BeekmanLabs.UnitTesting;
33
using InEngine.Commands;
4-
using InEngine.Core.Commands;
54
using InEngine.Core.Scheduling;
6-
using Moq;
75
using NUnit.Framework;
8-
using Quartz;
96

107
namespace InEngine.Core.Test.Scheduling
118
{
@@ -33,5 +30,18 @@ public void ShouldScheduleToRunLambdaEverySecond()
3330

3431
Subject.Job(() => { Console.WriteLine("Hello, world!"); }).EverySecond();
3532
}
33+
34+
[Test]
35+
public void ShouldScheduleCommandChain()
36+
{
37+
var alwaysSucceed = new AlwaysSucceed();
38+
39+
Subject.Job(new [] {
40+
new AlwaysSucceed(),
41+
new AlwaysSucceed(),
42+
new AlwaysSucceed(),
43+
new AlwaysSucceed()
44+
}).EverySecond();
45+
}
3646
}
3747
}

src/InEngine.Core/AbstractCommand.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void UpdateProgress(int tick)
4848
#endregion
4949

5050
#region Scheduling
51-
public void Execute(IJobExecutionContext context)
51+
public virtual void Execute(IJobExecutionContext context)
5252
{
5353
var properties = GetType().GetProperties();
5454
context.MergedJobDataMap.ToList().ForEach(x => {
@@ -68,7 +68,6 @@ public void Execute(IJobExecutionContext context)
6868
Failed(exception);
6969
}
7070
}
71-
7271
#endregion
7372

7473
#region Console output
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using InEngine.Core.Exceptions;
5+
using Quartz;
6+
7+
namespace InEngine.Core.Commands
8+
{
9+
public class Chain : AbstractCommand
10+
{
11+
public IList<AbstractCommand> Commands { get; set; } = new List<AbstractCommand>();
12+
13+
public override void Run()
14+
{
15+
Commands.ToList().ForEach(x => {
16+
try
17+
{
18+
x.Run();
19+
}
20+
catch (Exception exception)
21+
{
22+
throw new CommandChainFailedException(x.Name, exception);
23+
}
24+
});
25+
}
26+
27+
public override void Execute(IJobExecutionContext context)
28+
{
29+
var properties = GetType().GetProperties();
30+
context.MergedJobDataMap.ToList().ForEach(x => {
31+
var property = properties.FirstOrDefault(y => y.Name == x.Key);
32+
if (property != null)
33+
property.SetValue(this, x.Value);
34+
});
35+
36+
try
37+
{
38+
ExecutionLifeCycle.FirePreActions(this);
39+
Run();
40+
ExecutionLifeCycle.FirePostActions(this);
41+
}
42+
catch (Exception exception)
43+
{
44+
Failed(exception);
45+
}
46+
}
47+
}
48+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace InEngine.Core.Exceptions
4+
{
5+
public class CommandChainFailedException : Exception
6+
{
7+
public CommandChainFailedException(string message, Exception innerException) : base(message, innerException)
8+
{
9+
}
10+
}
11+
}

src/InEngine.Core/Queuing/Clients/DatabaseClient.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ public void Publish(ICommand command)
3333
}
3434
}
3535

36-
public void Publish(Action action)
37-
{
38-
Publish(new Lambda() { Action = action });
39-
}
40-
4136
public bool Consume()
4237
{
4338

src/InEngine.Core/Queuing/Clients/FileClient.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ public void Publish(ICommand command)
5959
}
6060
}
6161

62-
public void Publish(Action action)
63-
{
64-
Publish(new Lambda() { Action = action });
65-
}
66-
6762
public bool Consume()
6863
{
6964
var fileInfo = new DirectoryInfo(PendingQueuePath)

src/InEngine.Core/Queuing/Clients/RedisClient.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ public class RedisClient : IQueueClient
2929
public bool UseCompression { get; set; }
3030
public int RedisDb { get; set; }
3131

32-
public void Publish(Action action)
33-
{
34-
Publish(new Lambda() { Action = action });
35-
}
36-
3732
public void Publish(ICommand command)
3833
{
3934
Redis.ListLeftPush(

0 commit comments

Comments
 (0)