コード例 #1
0
        public void MetricsCompressorTests()
        {
            // compress bytes
            byte[] bytes;
            using (var compressor = new MetricsCompressor())
            {
                compressor.Append(new Metric <long>
                {
                    Descriptor = new MetricDescriptor <long>("name"),
                    Value      = 42
                });
                bytes = compressor.GetBytesAndReset();
            }

            // determine solution path
            var assemblyLocation = GetType().Assembly.Location;
            var solutionPath     = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(assemblyLocation), "../../../../.."));

            // name a temp directory
            var tempPath = Path.Combine(Path.GetTempPath(), $"hz-tests-{Guid.NewGuid():N}");

            try
            {
                // create the temp directory and copy the source files
                Directory.CreateDirectory(tempPath);
                //File.WriteAllText(Path.Combine(tempPath, "Program.java"), Resources.Java_CloudTests_Program);
                //File.WriteAllText(Path.Combine(tempPath, "TestConsumer.java"), Resources.Java_Cloudtests_TestConsumer);
                File.WriteAllText(Path.Combine(tempPath, "Program.java"), TestFiles.ReadAllText(this, "Java/CloudTests/Program.java"));
                File.WriteAllText(Path.Combine(tempPath, "TestConsumer.java"), TestFiles.ReadAllText(this, "Java/Cloudtests/TestConsumer.java"));

                // validate that we have the server JAR
                var serverJarPath = Path.Combine(solutionPath, $"temp/lib/hazelcast-{ServerVersion.GetVersion(NuGetVersion.Parse("4.0"))}.jar");
                Assert.That(File.Exists(serverJarPath), Is.True, $"Could not find JAR file {serverJarPath}");

                // compile
                Console.WriteLine("Compile...");
                Assert.That(Directory.GetFiles(tempPath, "*.java").Any(), "Could not find source files.");

                var p = Process.Start(new ProcessStartInfo(Path.Combine(JdkPath, "bin/javac.exe"), $"-cp {serverJarPath} {Path.Combine(tempPath, "*.java")}")
                                      .WithRedirects(true, true, false));
                Assert.That(p, Is.Not.Null);
                p.WaitForExit();
                Console.WriteLine($"Compilation exit code: {p.ExitCode}");
                Console.WriteLine("Compilation stderr:");
                Console.WriteLine(p.StandardError.ReadToEnd());
                Console.WriteLine("Compilation stdout:");
                Console.WriteLine(p.StandardOutput.ReadToEnd());
                Assert.That(p.ExitCode, Is.Zero, "Compilation failed.");

                // execute
                Console.WriteLine("Execute...");
                Console.WriteLine($"Writing {bytes.Length} bytes to java");
                p = Process.Start(new ProcessStartInfo(Path.Combine(JdkPath, "bin/java.exe"), $"-cp {serverJarPath};{tempPath} Program")
                                  .WithRedirects(true, true, true));
                Assert.That(p, Is.Not.Null);
                p.StandardInput.BaseStream.Write(bytes, 0, bytes.Length);
                p.StandardInput.Close();
                p.WaitForExit();
                Console.WriteLine($"Execution exit code: {p.ExitCode}");
                Console.WriteLine("Execution stderr:");
                Console.WriteLine(p.StandardError.ReadToEnd());
                Console.WriteLine("Execution stdout:");
                var output = p.StandardOutput.ReadToEnd();
                Console.WriteLine(output);
                Assert.That(p.ExitCode, Is.Zero, "Execution failed.");

                // assert that things were properly decompressed
                Assert.That(output.Contains("name = 42"));
            }
            finally
            {
                // get rid of the temp directory
                Directory.Delete(tempPath, true);
            }
        }