|
| 1 | +// Copyright (c) "Neo4j" |
| 2 | +// Neo4j Sweden AB [https://neo4j.com] |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | +// You may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using System.Threading.Tasks; |
| 19 | +using Neo4j.Driver.Internal.Mapping; |
| 20 | +using Xunit; |
| 21 | +using Xunit.Abstractions; |
| 22 | + |
| 23 | +namespace Neo4j.Driver.Tests.Mapping; |
| 24 | + |
| 25 | +public class MappingConcurrencyTests(ITestOutputHelper testOutputHelper) |
| 26 | +{ |
| 27 | + private interface ITestTask |
| 28 | + { |
| 29 | + Task Start(); |
| 30 | + } |
| 31 | + |
| 32 | + private class TestTask<T> : ITestTask |
| 33 | + { |
| 34 | + public Task Start() |
| 35 | + { |
| 36 | + return Task.Run( |
| 37 | + () => |
| 38 | + { |
| 39 | + for (var i = 0; i < 50; i++) |
| 40 | + { |
| 41 | + DefaultMapper.Get<T>(); |
| 42 | + DefaultMapper.Reset(); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private record DummyType1(string Name, int Age); |
| 49 | + private record DummyType2(string Name, int Age); |
| 50 | + private record DummyType3(string Name, int Age); |
| 51 | + private record DummyType4(string Name, int Age); |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async void DefaultMapperShouldBeThreadSafe() |
| 55 | + { |
| 56 | + List<ITestTask> threads = |
| 57 | + [ |
| 58 | + new TestTask<DummyType1>(), |
| 59 | + new TestTask<DummyType2>(), |
| 60 | + new TestTask<DummyType3>(), |
| 61 | + new TestTask<DummyType4>() |
| 62 | + ]; |
| 63 | + |
| 64 | + // wait for all threads to finish |
| 65 | + await Task.WhenAll(threads.Select(t => t.Start())); |
| 66 | + |
| 67 | + testOutputHelper.WriteLine("All threads finished."); |
| 68 | + } |
| 69 | +} |
0 commit comments