Skip to content

Commit

Permalink
It no longer supports datetime checking.
Browse files Browse the repository at this point in the history
add JobHandler.cs
  • Loading branch information
nameofSEOKWONHONG committed Oct 8, 2024
1 parent 7d2a5ca commit 7805a3e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 30 deletions.
49 changes: 34 additions & 15 deletions src/Job/JobHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,50 @@ private JobHandler()
public T Dequeue() => _queue.TryDequeue(out var result) ? result : default;
}

public class JobProsessor<T>
public class JobProcessor<T>
{
private static Lazy<JobProsessor<T>> _instance = new Lazy<JobProsessor<T>>(() => new JobProsessor<T>());
public static JobProsessor<T> Instance => _instance.Value;
private static Lazy<JobProcessor<T>> _instance = new Lazy<JobProcessor<T>>(() => new JobProcessor<T>());
public static JobProcessor<T> Instance => _instance.Value;


private ConcurrentDictionary<JobHandler<T>, Action<T>> _concurrentDictionary;
private JobHandler<T> _handler;
private Action<T> _action;
private bool _isRunning;

private Thread _thread;
private JobProsessor()
private JobProcessor()
{
_concurrentDictionary = new();
_thread = new Thread(StartProess);
_thread = new Thread(Start);
_thread.IsBackground = true;
_thread.Start();
}

public void SetProessor(JobHandler<T> jobHandler, Action<T> callback)
public void SetProcessor(JobHandler<T> jobHandler, Action<T> callback)
{
_concurrentDictionary.TryAdd(jobHandler, callback);
_handler = jobHandler;
_action = callback;
}

public void StartProess()
private void Start()
{
while (true)
_isRunning = true;

while (_isRunning)
{
try
{
if (_concurrentDictionary.TryGetValue(out var hander))
if (_handler.xIsNotEmpty())
{
var item = hander.Dequeue();
if (item.xAs<int>() > 0)
if (_action.xIsNotEmpty())
{
Console.WriteLine(item.xAs<int>());
var item = _handler.Dequeue();
if (item.xIsNotEmpty())
{
_action(item);
}
}
}
Thread.Sleep(10);
}
catch (InvalidOperationException e)
{
Expand All @@ -61,4 +69,15 @@ public void StartProess()
}
}
}

public void Stop()
{
_isRunning = false;
}

internal class ProcessItem
{
public JobHandler<T> JobHandler { get; set; }
public Action<T> Callback { get; set; }
}
}
38 changes: 38 additions & 0 deletions test/JobProcessorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Linq;
using System.Threading.Tasks;
using eXtensionSharp.Job;
using NUnit.Framework;

namespace eXtensionSharp.test;

public class JobProcessorTest
{
[Test]
public async Task job_processor_test()
{
JobProcessor<string>.Instance.SetProcessor(JobHandler<string>.Instance, item =>
{
if(item.xIsNotEmpty()) TestContext.WriteLine(item);
});
JobProcessor<int>.Instance.SetProcessor(JobHandler<int>.Instance, item =>
{
if(item > 0) TestContext.WriteLine(item);
});

var texts = "hello world";
var numbers = new[] { 1, 2, 3, 4, 5 };

Parallel.ForEach(texts.ToArray(), item =>
{
JobHandler<string>.Instance.Enqueue(item.ToString());
});
Parallel.ForEach(numbers, item =>
{
JobHandler<int>.Instance.Enqueue(item);
});

await Task.Delay(5000);

JobProcessor<int>.Instance.Stop();
}
}
6 changes: 4 additions & 2 deletions test/XEmtpyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public void is_empty_test()
{
var str1 = string.Empty;
string str2 = null;
DateTime dt = DateTime.MinValue;

//It no longer supports datetime checking.
//DateTime dt = DateTime.MinValue;
Assert.Multiple(() =>
{
Assert.That(str1.xIsEmpty(), Is.True);
Assert.That(str2.xIsEmpty(), Is.True);
Assert.That(dt.xIsEmpty(), Is.True);
//Assert.That(dt.xIsEmpty(), Is.True);
});
}

Expand Down
13 changes: 0 additions & 13 deletions test/xForEachTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,5 @@ public void xforeach_product_test()
Assert.That(c, Is.EqualTo(items3[c - 7]));
});
}

[Test]
public async Task processor_test()
{
JobProsessor<int>.Instance.SetProessor(JobHandler<int>.Instance);

JobHandler<int>.Instance.Enqueue(1);
JobHandler<int>.Instance.Enqueue(2);
JobHandler<int>.Instance.Enqueue(3);
JobHandler<int>.Instance.Enqueue(4);

await Task.Delay(5000 * 2);
}
}
}

0 comments on commit 7805a3e

Please sign in to comment.