public async Task MultipleCaches()
        {
            const string CacheName1 = "test1";
            const string CacheName2 = "test2";

            using (var testDirectory0 = new DisposableDirectory(FileSystem))
                using (var testDirectory1 = new DisposableDirectory(FileSystem))
                    using (var testDirectory2 = new DisposableDirectory(FileSystem))
                    {
                        var config = CreateStoreConfiguration();

                        var rootPath1 = testDirectory1.Path;
                        config.Write(FileSystem, rootPath1);

                        var rootPath2 = testDirectory2.Path;
                        config.Write(FileSystem, rootPath2);

                        var grpcPort         = PortExtensions.GetNextAvailablePort();
                        var grpcPortFileName = Guid.NewGuid().ToString();

                        var serviceConfiguration = new ServiceConfiguration(
                            new Dictionary <string, AbsolutePath> {
                            { CacheName1, rootPath1 }, { CacheName2, rootPath2 }
                        },
                            testDirectory0.Path,
                            ServiceConfiguration.DefaultGracefulShutdownSeconds,
                            grpcPort,
                            grpcPortFileName);

                        using (var server = CreateServer(serviceConfiguration))
                        {
                            var factory   = new MemoryMappedFileGrpcPortSharingFactory(Logger, grpcPortFileName);
                            var reader    = factory.GetPortReader();
                            var port      = reader.ReadPort();
                            var rpcConfig = new ServiceClientRpcConfiguration(port);

                            using (var store1 = new ServiceClientContentStore(
                                       Logger,
                                       FileSystem,
                                       new ServiceClientContentStoreConfiguration(CacheName1, rpcConfig, Scenario)))
                                using (var store2 = new ServiceClientContentStore(
                                           Logger,
                                           FileSystem,
                                           new ServiceClientContentStoreConfiguration(CacheName2, rpcConfig, Scenario)))
                                {
                                    try
                                    {
                                        var rs = await server.StartupAsync(_context);

                                        rs.ShouldBeSuccess();

                                        var storeBoolResult1 = await store1.StartupAsync(_context);

                                        storeBoolResult1.ShouldBeSuccess();

                                        var storeBoolResult2 = await store2.StartupAsync(_context);

                                        storeBoolResult2.ShouldBeSuccess();

                                        IContentSession session1 = null;
                                        IContentSession session2 = null;

                                        try
                                        {
                                            var createSessionResult1 = store1.CreateSession(_context, "session1", ImplicitPin.None);
                                            createSessionResult1.ShouldBeSuccess();

                                            var createSessionResult2 = store2.CreateSession(_context, "session2", ImplicitPin.None);
                                            createSessionResult2.ShouldBeSuccess();

                                            using (createSessionResult1.Session)
                                                using (createSessionResult2.Session)
                                                {
                                                    var r1 = await createSessionResult1.Session.StartupAsync(_context);

                                                    r1.ShouldBeSuccess();
                                                    session1 = createSessionResult1.Session;

                                                    var r2 = await createSessionResult2.Session.StartupAsync(_context);

                                                    r2.ShouldBeSuccess();
                                                    session2 = createSessionResult2.Session;

                                                    var r3 = await session1.PutRandomAsync(
                                                        _context,
                                                        ContentHashType,
                                                        false,
                                                        RandomContentByteCount,
                                                        Token);

                                                    var pinResult = await session1.PinAsync(_context, r3.ContentHash, Token);

                                                    pinResult.ShouldBeSuccess();

                                                    r3 = await session2.PutRandomAsync(
                                                        _context,
                                                        ContentHashType,
                                                        false,
                                                        RandomContentByteCount,
                                                        Token);

                                                    pinResult = await session2.PinAsync(_context, r3.ContentHash, Token);

                                                    pinResult.ShouldBeSuccess();
                                                }
                                        }
                                        finally
                                        {
                                            if (session2 != null)
                                            {
                                                await session2.ShutdownAsync(_context).ShouldBeSuccess();
                                            }

                                            if (session1 != null)
                                            {
                                                await session1.ShutdownAsync(_context).ShouldBeSuccess();
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        BoolResult r1 = null;
                                        BoolResult r2 = null;

                                        if (store1.StartupCompleted)
                                        {
                                            r1 = await store1.ShutdownAsync(_context);
                                        }

                                        if (store2.StartupCompleted)
                                        {
                                            r2 = await store2.ShutdownAsync(_context);
                                        }

                                        var r3 = await server.ShutdownAsync(_context);

                                        r1?.ShouldBeSuccess();
                                        r2?.ShouldBeSuccess();
                                        r3?.ShouldBeSuccess();
                                    }
                                }
                        }
                    }
        }
Beispiel #2
0
        public async Task ServiceTestAsync()
        {
            using var fileSystem = new PassThroughFileSystem(Logger);
            using var dir        = new DisposableDirectory(fileSystem);
            var cacheDir = dir.Path / "cache";
            var dataPath = dir.Path / "data";

            var args = new Dictionary <string, string>
            {
                ["paths"]            = cacheDir.Path,
                ["names"]            = "Default",
                ["grpcPort"]         = "7090",
                ["LogSeverity"]      = "Diagnostic",
                ["dataRootPath"]     = dataPath.Path,
                ["Scenario"]         = "AppTests",
                ["grpcPortFileName"] = "AppTestsMMF"
            };

            var serviceProcess = RunService("Service", args, Logger);

            try
            {
                await RunAppAsync("ServiceRunning", new Dictionary <string, string> {
                    { "waitSeconds", "5" }, { "Scenario", "AppTests" }
                }, Logger);

                var context = new Context(Logger);

                var config = new ServiceClientContentStoreConfiguration("Default", new ServiceClientRpcConfiguration {
                    GrpcPort = 7090
                }, scenario: "AppTests");
                using var store = new ServiceClientContentStore(Logger, fileSystem, config);
                await store.StartupAsync(context).ShouldBeSuccess();

                var sessionResult = store.CreateSession(context, "Default", ImplicitPin.None).ShouldBeSuccess();
                using var session = sessionResult.Session;
                await session.StartupAsync(context).ShouldBeSuccess();

                var source   = dir.Path / "source.txt";
                var contents = new byte[1024];
                Random.NextBytes(contents);
                fileSystem.WriteAllBytes(source, contents);

                var putResult = await session.PutFileAsync(context, HashType.MD5, source, FileRealizationMode.Any, CancellationToken.None).ShouldBeSuccess();

                var hash = putResult.ContentHash;

                await session.PinAsync(context, hash, CancellationToken.None).ShouldBeSuccess();

                var destination = dir.Path / "destination.txt";
                await session.PlaceFileAsync(
                    context,
                    hash,
                    destination,
                    FileAccessMode.ReadOnly,
                    FileReplacementMode.FailIfExists,
                    FileRealizationMode.Any,
                    CancellationToken.None).ShouldBeSuccess();

                fileSystem.ReadAllBytes(destination).Should().BeEquivalentTo(contents);
            }
            finally
            {
                if (!serviceProcess.HasExited)
                {
                    serviceProcess.Kill();
#pragma warning disable AsyncFixer02 // WaitForExitAsync should be used instead
                    serviceProcess.WaitForExit();
#pragma warning restore AsyncFixer02
                }
            }
        }