|
| 1 | +/* Copyright 2020-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq; |
| 19 | +using System.Threading; |
| 20 | +using MongoDB.Bson; |
| 21 | +using MongoDB.Bson.TestHelpers; |
| 22 | +using MongoDB.Bson.TestHelpers.JsonDrivenTests; |
| 23 | +using MongoDB.Driver; |
| 24 | +using MongoDB.Driver.Core; |
| 25 | +using MongoDB.Driver.TestHelpers; |
| 26 | +using MongoDB.Driver.Tests.Specifications.Runner; |
| 27 | + |
| 28 | +namespace WorkloadExecutor |
| 29 | +{ |
| 30 | + public class AstrolabeTestRunner : MongoClientJsonDrivenTestRunnerBase |
| 31 | + { |
| 32 | + // private fields |
| 33 | + private readonly CancellationToken _cancellationToken; |
| 34 | + private readonly Action _incrementOperationSuccesses; |
| 35 | + private readonly Action _incrementOperationErrors; |
| 36 | + private readonly Action _incrementOperationFailures; |
| 37 | + |
| 38 | + // protected properties |
| 39 | + protected override string[] ExpectedSharedColumns => new[] { "_path", "database", "collection", "testData", "tests" }; |
| 40 | + protected override string[] ExpectedTestColumns => new[] { "operations", "async" }; |
| 41 | + |
| 42 | + public AstrolabeTestRunner( |
| 43 | + Action incrementOperationSuccesses, |
| 44 | + Action incrementOperationErrors, |
| 45 | + Action incrementOperationFailures, |
| 46 | + CancellationToken cancellationToken) |
| 47 | + { |
| 48 | + _incrementOperationSuccesses = incrementOperationSuccesses; |
| 49 | + _incrementOperationErrors = incrementOperationErrors; |
| 50 | + _incrementOperationFailures = incrementOperationFailures; |
| 51 | + _cancellationToken = cancellationToken; |
| 52 | + } |
| 53 | + |
| 54 | + protected override string DatabaseNameKey => "database"; |
| 55 | + protected override string CollectionNameKey => "collection"; |
| 56 | + protected override string DataKey => "testData"; |
| 57 | + |
| 58 | + // public methods |
| 59 | + public void Run(JsonDrivenTestCase testCase) |
| 60 | + { |
| 61 | + SetupAndRunTest(testCase); |
| 62 | + } |
| 63 | + |
| 64 | + // protected methods |
| 65 | + protected override void AssertOperation(JsonDrivenTest test) |
| 66 | + { |
| 67 | + var actualException = test._actualException(); |
| 68 | + if (test._expectedException() == null) |
| 69 | + { |
| 70 | + if (actualException != null) |
| 71 | + { |
| 72 | + if (!(actualException is OperationCanceledException)) |
| 73 | + { |
| 74 | + Console.WriteLine($"Operation error (unexpected exception type): {actualException.GetType()}"); |
| 75 | + _incrementOperationErrors(); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + Console.WriteLine($"Operation cancelled: {actualException}"); |
| 80 | + } |
| 81 | + |
| 82 | + return; |
| 83 | + } |
| 84 | + if (test._expectedResult() == null) |
| 85 | + { |
| 86 | + _incrementOperationSuccesses(); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + try |
| 91 | + { |
| 92 | + test.AssertResult(); |
| 93 | + _incrementOperationSuccesses(); |
| 94 | + } |
| 95 | + catch (Exception ex) |
| 96 | + { |
| 97 | + Console.WriteLine($"Operation failure (unexpected exception type): {ex.GetType()}"); |
| 98 | + _incrementOperationFailures(); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + if (actualException == null) |
| 105 | + { |
| 106 | + _incrementOperationErrors(); |
| 107 | + |
| 108 | + return; |
| 109 | + } |
| 110 | + try |
| 111 | + { |
| 112 | + test.AssertException(); |
| 113 | + _incrementOperationSuccesses(); |
| 114 | + } |
| 115 | + catch |
| 116 | + { |
| 117 | + _incrementOperationFailures(); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + protected override void RunTest(BsonDocument shared, BsonDocument test, EventCapturer eventCapturer) |
| 123 | + { |
| 124 | + Console.WriteLine("dotnet astrolabetestrunner> creating disposable client..."); |
| 125 | + using (var client = CreateDisposableMongoClient(eventCapturer)) |
| 126 | + { |
| 127 | + Console.WriteLine("dotnet astrolabetestrunner> looping until cancellation is requested..."); |
| 128 | + while (!_cancellationToken.IsCancellationRequested) |
| 129 | + { |
| 130 | + // Clone because inserts will auto assign an id to the test case document |
| 131 | + ExecuteOperations( |
| 132 | + client: client, |
| 133 | + objectMap: new Dictionary<string, object>(), |
| 134 | + test: test.DeepClone().AsBsonDocument); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // private methods |
| 140 | + private DisposableMongoClient CreateDisposableMongoClient(EventCapturer eventCapturer) |
| 141 | + { |
| 142 | + var connectionString = Environment.GetEnvironmentVariable("MONGODB_URI"); |
| 143 | + var settings = MongoClientSettings.FromConnectionString(connectionString); |
| 144 | + if (eventCapturer != null) |
| 145 | + { |
| 146 | + settings.ClusterConfigurator = c => c.Subscribe(eventCapturer); |
| 147 | + } |
| 148 | + |
| 149 | + return new DisposableMongoClient(new MongoClient(settings)); |
| 150 | + } |
| 151 | + |
| 152 | + // nested types |
| 153 | + internal class TestCaseFactory : JsonDrivenTestCaseFactory |
| 154 | + { |
| 155 | + public JsonDrivenTestCase CreateTestCase(BsonDocument driverWorkload, bool async) |
| 156 | + { |
| 157 | + JsonDrivenHelper.EnsureAllFieldsAreValid(driverWorkload, new[] { "database", "collection", "testData", "operations" }); |
| 158 | + |
| 159 | + var adaptedDriverWorkload = new BsonDocument |
| 160 | + { |
| 161 | + { "_path", "Astrolabe command line arguments" }, |
| 162 | + { "database", driverWorkload["database"] }, |
| 163 | + { "collection", driverWorkload["collection"] }, |
| 164 | + { "tests", new BsonArray(new [] { new BsonDocument("operations", driverWorkload["operations"]) }) } |
| 165 | + }; |
| 166 | + if (driverWorkload.Contains("testData")) |
| 167 | + { |
| 168 | + adaptedDriverWorkload.Add("testData", driverWorkload["testData"]); |
| 169 | + } |
| 170 | + var testCase = CreateTestCases(adaptedDriverWorkload).Single(); |
| 171 | + testCase.Test["async"] = async; |
| 172 | + |
| 173 | + return testCase; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + internal static class JsonDrivenTestReflector |
| 179 | + { |
| 180 | + public static Exception _actualException(this JsonDrivenTest test) |
| 181 | + { |
| 182 | + return (Exception)Reflector.GetFieldValue(test, nameof(_actualException)); |
| 183 | + } |
| 184 | + |
| 185 | + public static BsonDocument _expectedException(this JsonDrivenTest test) |
| 186 | + { |
| 187 | + return (BsonDocument)Reflector.GetFieldValue(test, nameof(_expectedException)); |
| 188 | + } |
| 189 | + |
| 190 | + public static BsonValue _expectedResult(this JsonDrivenTest test) |
| 191 | + { |
| 192 | + return (BsonValue)Reflector.GetFieldValue(test, nameof(_expectedResult)); |
| 193 | + } |
| 194 | + |
| 195 | + public static void AssertException(this JsonDrivenTest test) |
| 196 | + { |
| 197 | + Reflector.Invoke(test, nameof(AssertException)); |
| 198 | + } |
| 199 | + |
| 200 | + public static void AssertResult(this JsonDrivenTest test) |
| 201 | + { |
| 202 | + Reflector.Invoke(test, nameof(AssertResult)); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments