forked from ravendb/ravendb
/
ConcurrentPatching.cs
56 lines (50 loc) · 1.77 KB
/
ConcurrentPatching.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Raven.Abstractions.Commands;
using Raven.Abstractions.Data;
using Raven.Json.Linq;
using Xunit;
namespace Raven.Tests
{
public class ConcurrentPatching : LocalClientTest
{
[Fact]
public void CanConcurrentlyUpdateSameDocument()
{
using (var store = NewDocumentStore("esent", false))
{
using(var s = store.OpenSession())
{
s.Store(new Post{Comments = new List<Comment>()});
s.SaveChanges();
}
int numberOfComments = 128;
var patches = Enumerable.Range(0, numberOfComments).Select(x =>
new PatchRequest
{
Name = "Comments",
Type = PatchCommandType.Add,
Value = RavenJToken.FromObject(new Comment
{
AuthorId = "ayende"
})
});
Parallel.ForEach(patches, data => store.DatabaseCommands.Patch("posts/1", new[]{data}));
using (var s = store.OpenSession())
{
Assert.Equal(numberOfComments, s.Load<Post>("posts/1").Comments.Count);
}
}
}
public class Post
{
public string Id { get; set; }
public IList<Comment> Comments { get; set; }
}
public class Comment
{
public string AuthorId { get; set; }
}
}
}