Skip to content

Commit

Permalink
Make all queries compatible with the LINQ V3 provider of MongoDB.Driv…
Browse files Browse the repository at this point in the history
…er (#339)

The latest version of MongoDB.Driver (2.19.0) changes the default LINQ provider from V2 to V3 any many queries were incompatible with the new provider before this commit, throwing MongoDB.Driver.Linq.ExpressionNotSupportedException.

Fixes #338
  • Loading branch information
0xced authored Feb 2, 2023
1 parent 31aa962 commit 543e8b4
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 168 deletions.
45 changes: 14 additions & 31 deletions src/Hangfire.Mongo.Tests/MongoConnectionFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public void CreateExpiredJob_CreatesAJobInTheStorage_AndSetsItsParameters()
Assert.NotNull(jobId);
Assert.NotEmpty(jobId);

var databaseJob = _dbContext.JobGraph
var document = _dbContext.JobGraph
.Find(new BsonDocument("_t", nameof(JobDto)))
.Project(b => new JobDto(b))
.ToList().Single();
.Single();
var databaseJob = new JobDto(document);
Assert.Equal(jobId, databaseJob.Id.ToString());
Assert.Equal(createdAt, databaseJob.CreatedAt);
Assert.Null(databaseJob.StateName);
Expand All @@ -148,10 +148,9 @@ public void CreateExpiredJob_CreatesAJobInTheStorage_AndSetsItsParameters()

var parameters = _dbContext
.JobGraph
.Find(new BsonDocument("_t", nameof(JobDto)){ ["_id"] = ObjectId.Parse(jobId) })
.Project(j => new JobDto(j))
.Find(new BsonDocument("_t", nameof(JobDto)) { ["_id"] = ObjectId.Parse(jobId) })
.ToList()
.SelectMany(j => j.Parameters)
.SelectMany(j => new JobDto(j).Parameters)
.ToDictionary(p => p.Key, x => x.Value);

Assert.NotNull(parameters);
Expand Down Expand Up @@ -326,11 +325,7 @@ public void SetParameters_CreatesNewParameter_WhenParameterWithTheGivenNameDoesN
["_id"] = jobId,
["_t"] = nameof(JobDto),
};
var job = _dbContext
.JobGraph
.Find(filter)
.Project(j => new JobDto(j))
.FirstOrDefault();
var job = new JobDto(_dbContext.JobGraph.Find(filter).First());

Assert.NotNull(job.Parameters);
Assert.Equal("Value", job.Parameters["Name"]);
Expand All @@ -357,11 +352,7 @@ public void SetParameter_UpdatesValue_WhenParameterWithTheGivenName_AlreadyExist
["_id"] = jobId,
["_t"] = nameof(JobDto),
};
var job = _dbContext
.JobGraph
.Find(filter)
.Project(j => new JobDto(j))
.FirstOrDefault();
var job = new JobDto(_dbContext.JobGraph.Find(filter).FirstOrDefault());

Assert.NotNull(job.Parameters);
Assert.Equal("AnotherValue", job.Parameters["Name"]);
Expand All @@ -387,11 +378,7 @@ public void SetParameter_CanAcceptNulls_AsValues()
["_id"] = jobId,
["_t"] = nameof(JobDto),
};
var job = _dbContext
.JobGraph
.Find(filter)
.Project(j => new JobDto(j))
.FirstOrDefault();
var job = new JobDto(_dbContext.JobGraph.Find(filter).FirstOrDefault());

Assert.NotNull(job.Parameters);
Assert.Null(job.Parameters["Name"]);
Expand Down Expand Up @@ -563,7 +550,7 @@ public void AnnounceServer_CreatesOrUpdatesARecord()
};
_connection.AnnounceServer("server", context1);

var server = _dbContext.Server.Find(new BsonDocument()).Project(b => new ServerDto(b)).Single();
var server = new ServerDto(_dbContext.Server.AsQueryable().Single());
Assert.Equal("server", server.Id);
Assert.Equal(context1.WorkerCount, server.WorkerCount);
Assert.Equal(context1.Queues, server.Queues);
Expand All @@ -576,7 +563,7 @@ public void AnnounceServer_CreatesOrUpdatesARecord()
WorkerCount = 1000
};
_connection.AnnounceServer("server", context2);
var sameServer = _dbContext.Server.Find(new BsonDocument()).Project(b => new ServerDto(b)).Single();
var sameServer = new ServerDto(_dbContext.Server.AsQueryable().Single());
Assert.Equal("server", sameServer.Id);
Assert.Equal(context2.WorkerCount, sameServer.WorkerCount);
}
Expand All @@ -603,7 +590,7 @@ public void RemoveServer_RemovesAServerRecord()

_connection.RemoveServer("Server1");

var server = _dbContext.Server.Find(new BsonDocument()).Project(b => new ServerDto(b)).ToList().Single();
var server = new ServerDto(_dbContext.Server.AsQueryable().Single());
Assert.NotEqual("Server1", server.Id, StringComparer.OrdinalIgnoreCase);
}

Expand Down Expand Up @@ -639,7 +626,7 @@ public void Heartbeat_UpdatesLastHeartbeat_OfTheServerWithGivenId()

_connection.Heartbeat("server1");

var servers = _dbContext.Server.Find(new BsonDocument()).Project(b => new ServerDto(b)).ToList()
var servers = _dbContext.Server.AsQueryable().ToList().Select(b => new ServerDto(b))
.ToDictionary(x => x.Id, x => x.LastHeartbeat);

Assert.True(servers.ContainsKey("server1"));
Expand Down Expand Up @@ -675,7 +662,7 @@ public void RemoveTimedOutServers_DoItsWorkPerfectly()

_connection.RemoveTimedOutServers(TimeSpan.FromHours(15));

var liveServer = _dbContext.Server.Find(new BsonDocument()).Project(b => new ServerDto(b)).Single();
var liveServer = new ServerDto(_dbContext.Server.AsQueryable().Single());
Assert.Equal("server2", liveServer.Id);
}

Expand Down Expand Up @@ -824,11 +811,7 @@ public void SetRangeInHash_MergesAllRecords()
[nameof(HashDto.Key)] = "some-hash",
["_t"] = nameof(HashDto)
};
var result = _dbContext.JobGraph
.Find(filter)
.Project(b => new HashDto(b))
.First()
.Fields;
var result = new HashDto(_dbContext.JobGraph.Find(filter).First()).Fields;

Assert.Equal("Value1", result["Key1"]);
Assert.Equal("Value2", result["Key2"]);
Expand Down
5 changes: 2 additions & 3 deletions src/Hangfire.Mongo.Tests/MongoDiscriminatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public void AddJob_FromWriteOnly_FetchOK()
{
["_t"] = nameof(JobDto),
["_id"] = ObjectId.Parse(jobId)
})
.Project(b => new JobDto(b))
.FirstOrDefault();
}).FirstOrDefault();


// ASSERT
Assert.NotNull(jobDto);
Expand Down
12 changes: 3 additions & 9 deletions src/Hangfire.Mongo.Tests/MongoDistributedLockFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,9 @@ public void Ctor_SetLockExpireAtWorks_WhenResourceIsNotLocked()
DateTime initialExpireAt = DateTime.UtcNow;
Thread.Sleep(TimeSpan.FromSeconds(5));

DistributedLockDto lockEntry = _database.DistributedLock
.Find(filter)
.Project(b => new DistributedLockDto(b)).FirstOrDefault();
var lockEntry = _database.DistributedLock.Find(filter).FirstOrDefault();
Assert.NotNull(lockEntry);
Assert.True(lockEntry.ExpireAt > initialExpireAt);
Assert.True(new DistributedLockDto(lockEntry).ExpireAt > initialExpireAt);
}
}

Expand All @@ -192,11 +190,7 @@ public void Ctor_AcquireLock_WhenLockExpired()
};
using (lock1.AcquireLock())
{
var lockEntry = _database
.DistributedLock
.Find(filter)
.Project(b => new DistributedLockDto(b))
.Single();
var lockEntry = new DistributedLockDto(_database.DistributedLock.Find(filter).Single());

Assert.True(lockEntry.ExpireAt > initialExpireAt);
}
Expand Down
17 changes: 7 additions & 10 deletions src/Hangfire.Mongo.Tests/MongoJobQueueFacts.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading;
using Hangfire.Mongo.Database;
using Hangfire.Mongo.Dto;
Expand Down Expand Up @@ -142,13 +143,10 @@ public void Dequeue_ShouldLeaveJobInTheQueue_ButSetItsFetchedAtValue()
["_t"] = nameof(JobDto),
["_id"] = ObjectId.Parse(payload.JobId)
};
var fetchedAt = _hangfireDbContext.JobGraph
.Find(filter)
.Project(b => new JobDto(b))
.FirstOrDefault()
.FetchedAt;
var document = _hangfireDbContext.JobGraph.Find(filter).FirstOrDefault();
var fetchedAt = new JobDto(document).FetchedAt;

Assert.NotNull(fetchedAt);
Assert.NotNull(document);
Assert.True(fetchedAt > DateTime.UtcNow.AddMinutes(-1));
_jobQueueSemaphoreMock.Verify(m => m.WaitNonBlock("default"), Times.Once);
}
Expand Down Expand Up @@ -248,11 +246,10 @@ public void Dequeue_ShouldSetFetchedAt_OnlyForTheFetchedJob()
["_t"] = nameof(JobDto),
["_id"] = new BsonDocument("$ne", ObjectId.Parse(payload.JobId))
};
var otherJob = _hangfireDbContext
.JobGraph.Find(filter).Project(b => new JobDto(b))
.FirstOrDefault();
Assert.NotNull(otherJob);
var document = _hangfireDbContext.JobGraph.Find(filter).FirstOrDefault();
Assert.NotNull(document);

var otherJob = new JobDto(document);
var otherJobFetchedAt = otherJob.FetchedAt;

Assert.Null(otherJobFetchedAt);
Expand Down
Loading

0 comments on commit 543e8b4

Please sign in to comment.