Skip to content

Commit

Permalink
Fix auto delete bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
hhalim committed Jun 20, 2017
1 parent 9728731 commit fa7c008
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 91 deletions.
2 changes: 1 addition & 1 deletion Shift.Cache.Redis/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.7")]
[assembly: AssemblyVersion("1.0.7.8")]
2 changes: 1 addition & 1 deletion Shift.DataLayer/JobDALDocumentDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ private async Task<int> DeleteAsync(int hours, ICollection<JobStatus?> statusLis
var whereQuery = string.Format("(j.Created != null AND j.Created < '{0}')", pastDate.ToString("o"));

//build where status
if (statusList != null)
if (statusList != null && statusList.Any())
{
var whereStatus = "";
foreach (var status in statusList)
Expand Down
4 changes: 2 additions & 2 deletions Shift.DataLayer/JobDALMongo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,12 @@ private async Task<int> DeleteAsync(int hours, ICollection<JobStatus?> statusLis
var blFilter = Builders<Job>.Filter;

var pastDate = DateTime.Now.AddHours(-hours);
//WARNING: The nullable Created field filter must use j.Created != null or it will CRASH the ToList action
//WARNING: The null-able Created field filter must use j.Created != null or it will CRASH the ToList action
var dateFilter = (!blFilter.Eq(j => j.Created, null) & blFilter.Lt(j => j.Created, pastDate.ToUniversalTime())); //use UTC for datetime filtering!

//build where status
FilterDefinition<Job> statusFilter = null;
if (statusList != null)
if (statusList != null && statusList.Any())
{
var listFilter = new List<FilterDefinition<Job>>();
foreach (var status in statusList)
Expand Down
2 changes: 1 addition & 1 deletion Shift.DataLayer/JobDALRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ private async Task<int> DeleteAsync(int hours, ICollection<JobStatus?> statusLis
var hashEntry = isSync ? RedisDatabase.HashGetAll(sortedSet.Element.ToString())
: await RedisDatabase.HashGetAllAsync(sortedSet.Element.ToString());
var job = RedisHelpers.ConvertFromRedis<Job>(hashEntry);
if(job != null && statusList.Contains(job.Status))
if(job != null && (statusList == null || !statusList.Any() || statusList.Contains(job.Status)) )
{
jobIDs.Add(job.JobID);
}
Expand Down
2 changes: 1 addition & 1 deletion Shift.DataLayer/JobDALSql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ private async Task<int> DeleteAsync(int hours, ICollection<JobStatus?> statusLis
var whereQuery = "j.Created < DATEADD(hour, -@hours, GETDATE())";

//build where status
if (statusList != null)
if (statusList != null && statusList.Any())
{
var whereStatus = "";
foreach (var status in statusList)
Expand Down
2 changes: 1 addition & 1 deletion Shift.DataLayer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.7")]
[assembly: AssemblyVersion("1.0.7.8")]
2 changes: 1 addition & 1 deletion Shift.Entities/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.7")]
[assembly: AssemblyVersion("1.0.7.8")]
64 changes: 54 additions & 10 deletions Shift.UnitTest.DataLayer/JobDALDocumentDBAsyncTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,14 @@ public async Task UpdateAsyncTest()

//Test auto delete older than 24 hours and Null(not started) status
[Fact]
public async Task DeleteAsyncOldJobsNotStarted()
public async Task DeleteAsyncOldJobs_NotStarted()
{
var job = new Job
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48)
};
job = await jobDAL.SetJobAsync(job);
Assert.True(!string.IsNullOrWhiteSpace(job.JobID));

var count = await jobDAL.DeleteAsync(24, new List<JobStatus?> { null });
var outJob = await jobDAL.GetJobAsync(job.JobID);
Expand All @@ -102,7 +101,7 @@ public async Task DeleteAsyncOldJobsNotStarted()

//Test auto delete older than 24 hours and with Error or Completed status
[Fact]
public async Task DeleteAsyncOldJobsErrorAndCompletedTest()
public async Task DeleteAsyncOldJobs_ErrorAndCompletedTest()
{
var job = new Job
{
Expand All @@ -112,14 +111,23 @@ public async Task DeleteAsyncOldJobsErrorAndCompletedTest()
Error = "Test delete old job with status: Error"
};
job = await jobDAL.SetJobAsync(job);
var job2 = new Job();
job2.AppID = AppID;
job2.Created = DateTime.Now.AddHours(-48);
job2.Status = JobStatus.Completed;
job2.Error = "Test delete old job with status: Completed";

var job2 = new Job()
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48),
Status = JobStatus.Completed,
Error = "Test delete old job with status: Completed"
};
job2 = await jobDAL.SetJobAsync(job2);
Assert.True(!string.IsNullOrWhiteSpace(job.JobID));
Assert.True(!string.IsNullOrWhiteSpace(job2.JobID));

var job3 = new Job()
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48),
Status = null
};
job3 = await jobDAL.SetJobAsync(job3);

var count = await jobDAL.DeleteAsync(24, new List<JobStatus?> { JobStatus.Error, JobStatus.Completed });
Assert.True(count > 0);
Expand All @@ -129,6 +137,42 @@ public async Task DeleteAsyncOldJobsErrorAndCompletedTest()

var outJob2 = await jobDAL.GetJobAsync(job2.JobID);
Assert.Null(outJob2);

var outJob3 = await jobDAL.GetJobAsync(job3.JobID);
await jobDAL.DeleteAsync(new List<string> { job3.JobID });
Assert.NotNull(outJob3);
Assert.Equal(job3.JobID, outJob3.JobID);
}

//Test auto delete older than 24 hours and don't care about status
[Fact]
public async Task DeleteAsyncOldJobs_AnyStatus()
{
var job = new Job
{
AppID = AppID,
Status = JobStatus.Completed,
Created = DateTime.Now.AddHours(-48)
};
job = await jobDAL.SetJobAsync(job);

var job2 = new Job()
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48),
Status = JobStatus.Error,
Error = "Test delete old job with Any Status"
};
job2 = await jobDAL.SetJobAsync(job2);

var count = await jobDAL.DeleteAsync(24, null);
Assert.True(count > 0);

var outJob = await jobDAL.GetJobAsync(job.JobID);
Assert.Null(outJob);

var outJob2 = await jobDAL.GetJobAsync(job2.JobID);
Assert.Null(outJob2);
}

[Fact]
Expand Down
64 changes: 54 additions & 10 deletions Shift.UnitTest.DataLayer/JobDALDocumentDBTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@ public void UpdateTest()

//Test auto delete older than 24 hours and Null(not started) status
[Fact]
public void DeleteOldJobsNotStarted()
public void DeleteOldJobs_NotStarted()
{
var job = new Job
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48)
};
job = jobDAL.SetJob(job);
Assert.True(!string.IsNullOrWhiteSpace(job.JobID));

var count = jobDAL.Delete(24, new List<JobStatus?> { null });
var outJob = jobDAL.GetJob(job.JobID);
Expand All @@ -100,7 +99,7 @@ public void DeleteOldJobsNotStarted()

//Test auto delete older than 24 hours and with Error or Completed status
[Fact]
public void DeleteOldJobsErrorAndCompletedTest()
public void DeleteOldJobs_ErrorAndCompletedTest()
{
var job = new Job
{
Expand All @@ -110,14 +109,23 @@ public void DeleteOldJobsErrorAndCompletedTest()
Error = "Test delete old job with status: Error"
};
job = jobDAL.SetJob(job);
var job2 = new Job();
job2.AppID = AppID;
job2.Created = DateTime.Now.AddHours(-48);
job2.Status = JobStatus.Completed;
job2.Error = "Test delete old job with status: Completed";

var job2 = new Job()
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48),
Status = JobStatus.Completed,
Error = "Test delete old job with status: Completed"
};
job2 = jobDAL.SetJob(job2);
Assert.True(!string.IsNullOrWhiteSpace(job.JobID));
Assert.True(!string.IsNullOrWhiteSpace(job2.JobID));

var job3 = new Job()
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48),
Status = null
};
job3 = jobDAL.SetJob(job3);

var count = jobDAL.Delete(24, new List<JobStatus?> { JobStatus.Error, JobStatus.Completed });
Assert.True(count > 0);
Expand All @@ -127,6 +135,42 @@ public void DeleteOldJobsErrorAndCompletedTest()

var outJob2 = jobDAL.GetJob(job2.JobID);
Assert.Null(outJob2);

var outJob3 = jobDAL.GetJob(job3.JobID);
jobDAL.Delete(new List<string> { job3.JobID });
Assert.NotNull(outJob3);
Assert.Equal(job3.JobID, outJob3.JobID);
}

//Test auto delete older than 24 hours and don't care about status
[Fact]
public void DeleteOldJobs_AnyStatus()
{
var job = new Job
{
AppID = AppID,
Status = JobStatus.Completed,
Created = DateTime.Now.AddHours(-48)
};
job = jobDAL.SetJob(job);

var job2 = new Job()
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48),
Status = JobStatus.Error,
Error = "Test delete old job with Any Status"
};
job2 = jobDAL.SetJob(job2);

var count = jobDAL.Delete(24, null);
Assert.True(count > 0);

var outJob = jobDAL.GetJob(job.JobID);
Assert.Null(outJob);

var outJob2 = jobDAL.GetJob(job2.JobID);
Assert.Null(outJob2);
}

[Fact]
Expand Down
64 changes: 54 additions & 10 deletions Shift.UnitTest.DataLayer/JobDALMongoAsyncTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,14 @@ public async Task UpdateAsyncTest()

//Test auto delete older than 24 hours and Null(not started) status
[Fact]
public async Task DeleteAsyncOldJobsNotStarted()
public async Task DeleteAsyncOldJobs_NotStarted()
{
var job = new Job
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48)
};
job = await jobDAL.SetJobAsync(job);
Assert.True(!string.IsNullOrWhiteSpace(job.JobID));

var count = await jobDAL.DeleteAsync(24, new List<JobStatus?> { null });
var outJob = await jobDAL.GetJobAsync(job.JobID);
Expand All @@ -101,7 +100,7 @@ public async Task DeleteAsyncOldJobsNotStarted()

//Test auto delete older than 24 hours and with Error or Completed status
[Fact]
public async Task DeleteAsyncOldJobsErrorAndCompletedTest()
public async Task DeleteAsyncOldJobs_ErrorAndCompletedTest()
{
var job = new Job
{
Expand All @@ -111,14 +110,23 @@ public async Task DeleteAsyncOldJobsErrorAndCompletedTest()
Error = "Test delete old job with status: Error"
};
job = await jobDAL.SetJobAsync(job);
var job2 = new Job();
job2.AppID = AppID;
job2.Created = DateTime.Now.AddHours(-48);
job2.Status = JobStatus.Completed;
job2.Error = "Test delete old job with status: Completed";

var job2 = new Job()
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48),
Status = JobStatus.Completed,
Error = "Test delete old job with status: Completed"
};
job2 = await jobDAL.SetJobAsync(job2);
Assert.True(!string.IsNullOrWhiteSpace(job.JobID));
Assert.True(!string.IsNullOrWhiteSpace(job2.JobID));

var job3 = new Job()
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48),
Status = null
};
job3 = await jobDAL.SetJobAsync(job3);

var count = await jobDAL.DeleteAsync(24, new List<JobStatus?> { JobStatus.Error, JobStatus.Completed });
Assert.True(count > 0);
Expand All @@ -128,6 +136,42 @@ public async Task DeleteAsyncOldJobsErrorAndCompletedTest()

var outJob2 = await jobDAL.GetJobAsync(job2.JobID);
Assert.Null(outJob2);

var outJob3 = await jobDAL.GetJobAsync(job3.JobID);
await jobDAL.DeleteAsync(new List<string> { job3.JobID });
Assert.NotNull(outJob3);
Assert.Equal(job3.JobID, outJob3.JobID);
}

//Test auto delete older than 24 hours and don't care about status
[Fact]
public async Task DeleteAsyncOldJobs_AnyStatus()
{
var job = new Job
{
AppID = AppID,
Status = JobStatus.Completed,
Created = DateTime.Now.AddHours(-48)
};
job = await jobDAL.SetJobAsync(job);

var job2 = new Job()
{
AppID = AppID,
Created = DateTime.Now.AddHours(-48),
Status = JobStatus.Error,
Error = "Test delete old job with Any Status"
};
job2 = await jobDAL.SetJobAsync(job2);

var count = await jobDAL.DeleteAsync(24, null);
Assert.True(count > 0);

var outJob = await jobDAL.GetJobAsync(job.JobID);
Assert.Null(outJob);

var outJob2 = await jobDAL.GetJobAsync(job2.JobID);
Assert.Null(outJob2);
}

[Fact]
Expand Down
Loading

0 comments on commit fa7c008

Please sign in to comment.