-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Repo issue 1413 #1414
base: main
Are you sure you want to change the base?
Repo issue 1413 #1414
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using Hangfire.MemoryStorage; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using Xunit; | ||
|
||
namespace Hangfire.Core.Tests | ||
{ | ||
public class JobWrapperNotWorking | ||
{ | ||
private readonly string RecurringJobId = Guid.NewGuid().ToString(); | ||
|
||
public Action Callback { get; set; } | ||
|
||
public void ScheduleAndTrigger() | ||
{ | ||
RecurringJob.AddOrUpdate(RecurringJobId, () => Invoke(), () => "* * * * *"); | ||
RecurringJob.Trigger(RecurringJobId); | ||
Thread.Sleep(1000); | ||
RecurringJob.RemoveIfExists(RecurringJobId); | ||
} | ||
|
||
public void Invoke() | ||
{ | ||
Callback?.Invoke(); | ||
} | ||
} | ||
|
||
public class JobWrapperWorking | ||
{ | ||
private readonly string RecurringJobId = Guid.NewGuid().ToString(); | ||
private static readonly IDictionary<string, JobWrapperWorking> s_jobId2WrapperMappings = new ConcurrentDictionary<string, JobWrapperWorking>(); | ||
|
||
public Action Callback { get; set; } | ||
|
||
public void ScheduleAndTrigger() | ||
{ | ||
s_jobId2WrapperMappings[RecurringJobId] = this; | ||
RecurringJob.AddOrUpdate(RecurringJobId, () => Invoke(RecurringJobId), () => "* * * * *"); | ||
RecurringJob.Trigger(RecurringJobId); | ||
Thread.Sleep(1000); | ||
RecurringJob.RemoveIfExists(RecurringJobId); | ||
} | ||
|
||
public void Invoke(string jobId) | ||
{ | ||
var wrapper = s_jobId2WrapperMappings[jobId]; | ||
wrapper?.Callback?.Invoke(); | ||
} | ||
} | ||
|
||
public class RecurringJobWithClassFacts | ||
{ | ||
static RecurringJobWithClassFacts() | ||
{ | ||
GlobalConfiguration.Configuration | ||
.UseMemoryStorage(); | ||
|
||
_ = new BackgroundJobServer(); | ||
} | ||
|
||
[Fact] | ||
public void CallbackShouldTrigger_NotWorking() | ||
{ | ||
var job = new JobWrapperNotWorking(); | ||
bool triggered = false; | ||
job.Callback = () => { triggered = true; }; | ||
|
||
job.ScheduleAndTrigger(); | ||
|
||
Assert.Equal(false, triggered); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So triggered is false here |
||
} | ||
|
||
[Fact] | ||
public void CallbackShouldTrigger_Working() | ||
{ | ||
var job = new JobWrapperWorking(); | ||
bool triggered = false; | ||
job.Callback = () => { triggered = true; }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while callback is invoked in this case |
||
|
||
job.ScheduleAndTrigger(); | ||
|
||
Assert.Equal(true, triggered); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and triggered is true here |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Callback here is not invoked