Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions tests/Hangfire.Core.Tests/Hangfire.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net452;net461;netcoreapp1.0;netcoreapp2.1</TargetFrameworks>
<NoWarn>0618</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='netcoreapp1.0'">
<NoWarn>$(NoWarn);1701</NoWarn>
</PropertyGroup>
Expand All @@ -22,6 +22,10 @@
<ProjectReference Include="..\..\src\Hangfire.Core\Hangfire.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Hangfire.MemoryStorage" Version="1.*" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
Expand All @@ -33,4 +37,4 @@
</ReferencePath>
</ItemGroup>
</Target>
</Project>
</Project>
87 changes: 87 additions & 0 deletions tests/Hangfire.Core.Tests/RecurringJobWithClassFacts.cs
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; };
Copy link
Author

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


job.ScheduleAndTrigger();

Assert.Equal(false, triggered);
Copy link
Author

Choose a reason for hiding this comment

The 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; };
Copy link
Author

Choose a reason for hiding this comment

The 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);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and triggered is true here

}
}
}
24 changes: 12 additions & 12 deletions tests/Hangfire.Core.Tests/Server/RecurringJobSchedulerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public RecurringJobSchedulerFacts()

_factory = new Mock<IBackgroundJobFactory>();
_factory.Setup(x => x.Create(It.IsAny<CreateContext>())).Returns(_backgroundJobMock.Object);

_stateMachine = new Mock<IStateMachine>();
_factory.SetupGet(x => x.StateMachine).Returns(_stateMachine.Object);
}
Expand All @@ -96,7 +96,7 @@ public RecurringJobSchedulerFacts()
public void Ctor_ThrowsAnException_WhenJobFactoryIsNull()
{
var exception = Assert.Throws<ArgumentNullException>(
// ReSharper disable once AssignNullToNotNullAttribute
// ReSharper disable once AssignNullToNotNullAttribute
() => new RecurringJobScheduler(null, _delay, _timeZoneResolver.Object, _nowInstantFactory));

Assert.Equal("factory", exception.ParamName);
Expand Down Expand Up @@ -209,7 +209,7 @@ public void Execute_UpdatesRecurringJobParameters_OnCompletion(bool useJobStorag
It.Is<Dictionary<string, string>>(rj =>
rj.ContainsKey("NextExecution") && rj["NextExecution"]
== JobHelper.SerializeDateTime(_nextInstant))));

_transaction.Verify(x => x.Commit());
}

Expand All @@ -235,7 +235,7 @@ public void Execute_UpdatesRecurringJobParameters_OnCompletion(bool useJobStorag
It.Is<Dictionary<string, string>>(rj =>
rj.ContainsKey("NextExecution") && rj["NextExecution"]
== JobHelper.SerializeDateTime(_nextInstant))));

_transaction.Verify(x => x.Commit());
}

Expand Down Expand Up @@ -276,7 +276,7 @@ public void Execute_RemovesRecurringJobFromSchedule_WhenHashDoesNotExist(bool us
_connection.SetupSequence(x => x.GetFirstByLowestScoreFromSet("recurring-jobs", 0, JobHelper.ToTimestamp(_nowInstant)))
.Returns("non-existing-job")
.Returns((string)null);
}
}

var scheduler = CreateScheduler();

Expand Down Expand Up @@ -367,7 +367,7 @@ public void Execute_DoesNotFixCreatedAtField_IfItExists(bool useJobStorageConnec

// Act
scheduler.Execute(_context.Object);

// Assert
_connection.Verify(
x => x.SetRangeInHash(
Expand Down Expand Up @@ -395,7 +395,7 @@ public void Execute_FixedMissingCreatedAtField(bool useJobStorageConnection)
$"recurring-job:{RecurringJobId}",
It.Is<Dictionary<string, string>>(rj => rj.ContainsKey("CreatedAt"))),
Times.Once);

_transaction.Verify(x => x.Commit());
}

Expand All @@ -422,7 +422,7 @@ public void Execute_UsesNextExecutionTime_WhenBothLastExecutionAndCreatedAtAreNo
It.Is<Dictionary<string, string>>(rj =>
rj.ContainsKey("LastExecution") && rj["LastExecution"]
== JobHelper.SerializeDateTime(_nowInstant))));

_transaction.Verify(x => x.Commit());
}

Expand Down Expand Up @@ -469,7 +469,7 @@ public void Execute_DoesNotEnqueueRecurringJob_WhenItIsCorrectAndItWasNotTrigger

// Act
scheduler.Execute(_context.Object);

// Assert
_stateMachine.Verify(x => x.ApplyState(It.IsAny<ApplyStateContext>()), Times.Never);
}
Expand Down Expand Up @@ -511,12 +511,12 @@ public void Execute_SchedulesNextExecution_AfterCreatingAJob(bool useJobStorageC
_transaction.Verify(x => x.SetRangeInHash(
$"recurring-job:{RecurringJobId}",
It.Is<Dictionary<string, string>>(rj =>
rj.ContainsKey("NextExecution") &&
rj.ContainsKey("NextExecution") &&
rj["NextExecution"] == JobHelper.SerializeDateTime(_nowInstant.AddMinutes(1)))));

_transaction.Verify(x => x.AddToSet(
"recurring-jobs",
"recurring-job-id",
"recurring-jobs",
"recurring-job-id",
JobHelper.ToTimestamp(_nowInstant.AddMinutes(1))));

_transaction.Verify(x => x.Commit());
Expand Down