Exemple #1
0
        static void PlayCafeBabeGame(FileInfo outputFile, TimeSpan gameMaxTime)
        {
            Result result;
            string bufferContents;

            using IBufferBasedOutputHelper outputHelper = OrderedThreadSafeTestOutputHelper.CreateInstance();
            {
                using IDeadBeefCafeGame game =
                          GameFactory.CreateDeadBeefCafeGame(outputHelper, 3, CafeBabeGame_GameEnded);
                Console.WriteLine("Concrete type of CafeBabe Game: [" + game.GetType().Name + "].");

                Result?temp = WaitForGameEndOrTimeout(gameMaxTime);
                result = temp ?? default;
            }


            try
            {
                bufferContents = outputHelper.GetCurrentTextAndClearBuffer(TimeSpan.FromSeconds(2));
            }
            catch (TimeoutException ex)
            {
                Console.Error.WriteLine(ex.ToString());
                string moreToSay =
                    "Unable to retrieve logs from the output helper " +
                    "because of timeout.  No results file can be written.";
                Console.Error.WriteLine(moreToSay);
                bufferContents = string.Empty;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
                string moreToSay = "Unable to retrieve logs from the output helper " +
                                   "because of unexpected exception.  No results file can be written.";
                Console.Error.WriteLine(moreToSay);
                bufferContents = string.Empty;
            }

            if (result == default)
            {
                Console.Error.WriteLine("The cafe babe game faulted.  No results can be retrieved.");
                Environment.Exit(-1);
                return;
            }

            DeadBeefCafeGameResult finalRes = result.GameResult ?? throw new InvalidOperationException();

            Console.WriteLine(finalRes.Success ? "The game was successful!" : "The game FAILED.");
            if (finalRes.Success)
            {
                bool validated = ValidateLog(bufferContents, finalRes.FinalArray);
                if (validated)
                {
                    Console.WriteLine("The results were validated.");
                }
                else
                {
                    Console.Error.WriteLine("THE RESULTS COULD NOT BE VALIDATED ... POSSIBLE FLAW IN VAULTS!");
                }
            }

            TimeSpan duration = finalRes.EndedAt - finalRes.StartedAt;

            Console.WriteLine("The game lasted {0:F3} milliseconds", duration.TotalMilliseconds);
            if (finalRes.Cancelled)
            {
                Console.WriteLine("The game was cancelled.");
            }
            else
            {
                Console.WriteLine("There were {0} xes and {1} oes.", finalRes.XCount, finalRes.OCount);
                Console.WriteLine(finalRes.WinningThreadIndex != null
                    ? ("The winning reader thread was thread at index " + finalRes.WinningThreadIndex.Value + ".")
                    : "There was no winning thread.");
            }

            Console.WriteLine("Writing final string and logs to file.");
            try
            {
                using (var sw = outputFile.CreateText())
                {
                    sw.WriteLine("The final array was:");
                    sw.WriteLine(result.ArrayText);
                    sw.WriteLine("END FINAL ARRAY");

                    if (!string.IsNullOrWhiteSpace(bufferContents))
                    {
                        sw.WriteLine();
                        sw.WriteLine("Log follows:");
                        sw.WriteLine(bufferContents);
                        sw.WriteLine("End LOG.");
                    }
                    else
                    {
                        sw.WriteLine();
                        sw.WriteLine("Log unavailable.");
                    }
                }
                Console.WriteLine("Successfully wrote to [" + outputFile.FullName + "].");
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("There was an error writing to file [" + outputFile.FullName +
                                        "]: contents: [" + ex + "].  File not written.");
            }
        }
Exemple #2
0
        private static void PlayMultipleCafeBabeGames(int count, FileInfo failureOutputFile, TimeSpan gameTimeLimit)
        {
            if (count < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(count), count, "Parameter not positive.");
            }
            StringBuilder allLog = new StringBuilder();

            for (int i = 1; i <= count; ++i)
            {
                using IBufferBasedOutputHelper outputHelper = OrderedThreadSafeTestOutputHelper.CreateInstance();
                {
                    Console.WriteLine($"Starting game {i} of {count}");
                    Result r;
                    using (GameFactory.CreateDeadBeefCafeGame(outputHelper, 3, CafeBabeGame_GameEnded))
                    {
                        r = WaitForGameEndOrTimeout(gameTimeLimit) ?? default;
                        if (r == default)
                        {
                            Console.Error.WriteLine(
                                $"FAILED: The cafe babe game# {i} of {count} faulted.  No results can be retrieved.");
                            string bufferContents = TryGetBufferContents(TimeSpan.FromSeconds(2), outputHelper);
                            LogContents(i, null, bufferContents, failureOutputFile);
                            return;
                        }

                        bool success = r.GameResult?.Success == true;
                        if (success)
                        {
                            Console.WriteLine($"Game {i} of {count} finished ok.");
                            string buffer = outputHelper.GetCurrentTextAndClearBuffer(TimeSpan.FromSeconds(2));
                            if (!ValidateLog(buffer, r.GameResult.Value.FinalArray))
                            {
                                Console.Error.WriteLine("Buffer validation failed.");
                                CafeBabeResultStorage storage = CafeBabeResultStorage.CreateStorageObject(in r, buffer);
                                SerializersDeserializers.SerializeObjectToFile(storage, failureOutputFile);
                                Console.Error.WriteLine("Failing contents written to output file: [" + failureOutputFile + "].");
                                throw new InvalidOperationException("Unable to validate buffer contents.");
                            }

                            allLog.AppendLine($"Game {i} of {count} finished ok.");
                        }
                        else
                        {
                            Console.WriteLine($"FAULT Game {i} of {count} FAULTED.");
                            if (r.GameResult?.Cancelled == true)
                            {
                                Console.WriteLine("The game was cancelled.");
                            }
                            else
                            {
                                Console.WriteLine("There were {0} xes and {1} oes.", r.GameResult?.XCount ?? -1,
                                                  r.GameResult?.XCount ?? -1);
                                Console.WriteLine(r.GameResult?.WinningThreadIndex != null
                                    ? ("The winning reader thread was thread at index " +
                                       r.GameResult?.WinningThreadIndex.Value + ".")
                                    : "There was no winning thread.");
                                string outputBuffer =
                                    TryGetBufferContents(TimeSpan.FromMilliseconds(500), outputHelper);
                                LogContents(i, in r, outputBuffer ?? string.Empty, failureOutputFile);
                            }

                            Environment.Exit(-1);
                            return;
                        }
                    }
                }

                void LogContents(int gameNum, in Result res, string buff, FileInfo of)
                {
                    Console.WriteLine($"Writing data to file for failed game# {gameNum}.");
                    try
                    {
                        SerializersDeserializers.SerializeObjectToFile(CafeBabeResultStorage.CreateStorageObject(in res, buff), of);
                        Console.WriteLine("Successfully wrote to [" + of.FullName + "].");
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine("There was an error writing to file [" + of.FullName +
                                                "]: contents: [" + ex + "].  File not written.");
                    }
                }

                string TryGetBufferContents(TimeSpan timeout, IBufferBasedOutputHelper helper)
                {
                    string ret;

                    try
                    {
                        ret = helper.GetCurrentTextAndClearBuffer(timeout);
                    }
                    catch (TimeoutException ex)
                    {
                        Console.Error.WriteLine(ex.ToString());
                        string moreToSay =
                            "Unable to retrieve logs from the output helper " +
                            "because of timeout.  No results file can be written.";
                        Console.Error.WriteLine(moreToSay);
                        ret = string.Empty;
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine(ex.ToString());
                        string moreToSay = "Unable to retrieve logs from the output helper " +
                                           "because of unexpected exception.  No results file can be written.";
                        Console.Error.WriteLine(moreToSay);
                        ret = string.Empty;
                    }

                    return(ret);
                }
            }
        }
Exemple #3
0
        private static void PlayMultipleClortonGames(int count, FileInfo outputFile)
        {
            if (count < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(count), count, "Parameter not positive.");
            }

            string    failingBufferContents = string.Empty;
            VaultType oddVaultType          = TheRng.Next(0, 2) == 0 ? VaultType.Basic : VaultType.Custom;
            VaultType evenVaultType         = oddVaultType == VaultType.Basic ? VaultType.Custom : VaultType.Basic;

            for (int i = 1; i <= count; ++i)
            {
                using IBufferBasedOutputHelper outputHelper = OrderedThreadSafeTestOutputHelper.CreateInstance();
                {
                    Console.WriteLine($"Starting game {i} of {count}");
                    ClortonGameResult?result;
                    var factory = GetGameFactory(i, evenVaultType);
                    using (IClortonGame game = factory.CreateClortonGame(outputHelper, 3, ClortonGame_GameEnded))
                    {
                        Console.WriteLine("Concrete type of clorton game: [" + game.GetType().Name + "].");
                        result = WaitForGameEndOrTimeout(TimeSpan.FromSeconds(3));

                        if (result == null)
                        {
                            Console.Error.WriteLine(
                                $"FAILED: The clorton game# {i} of {count} faulted.  No results can be retrieved.");
                            string bufferContents = TryGetBufferContents(TimeSpan.FromSeconds(2), outputHelper);
                            LogContents(i, null, bufferContents, outputFile);
                            return;
                        }

                        ClortonGameResult finalRes = result.Value;
                        bool success = finalRes.Success && ValidateFinalResult(in finalRes);
                        if (success)
                        {
                            Console.WriteLine($"Game {i} of {count} finished ok.");
                        }
                        else
                        {
                            Console.WriteLine($"FAULT Game {i} of {count} FAULTED.");
                            if (finalRes.Cancelled)
                            {
                                Console.WriteLine("The game was cancelled.");
                            }
                            else
                            {
                                Console.WriteLine("There were {0} xes and {1} oes.", finalRes.XCount,
                                                  finalRes.OCount);
                                Console.WriteLine(finalRes.WinningThreadIndex != null
                                    ? ("The winning reader thread was thread at index " +
                                       finalRes.WinningThreadIndex.Value + ".")
                                    : "There was no winning thread.");
                            }
                            Environment.Exit(-1);
                            return;
                        }
                    }
                }
            }

            void LogContents(int gameNum, in ClortonGameResult?res, string buff, FileInfo of)
            {
                Console.WriteLine($"Writing final string and logs to file for failed game# {gameNum}.");
                try
                {
                    using (var sw = of.CreateText())
                    {
                        sw.WriteLine("The final string was:");
                        sw.WriteLine(res?.FinalString ?? string.Empty);
                        sw.WriteLine("END FINAL STRING");

                        if (!string.IsNullOrWhiteSpace(buff))
                        {
                            sw.WriteLine();
                            sw.WriteLine("Log follows:");
                            sw.WriteLine(buff);
                            sw.WriteLine("End LOG.");
                        }
                        else
                        {
                            sw.WriteLine();
                            sw.WriteLine("Log unavailable.");
                        }
                    }

                    Console.WriteLine("Successfully wrote to [" + outputFile.FullName + "].");
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("There was an error writing to file [" + outputFile.FullName +
                                            "]: contents: [" + ex + "].  File not written.");
                }
            }

            string TryGetBufferContents(TimeSpan timeout, IBufferBasedOutputHelper helper)
            {
                string ret;

                try
                {
                    ret = helper.GetCurrentTextAndClearBuffer(timeout);
                }
                catch (TimeoutException ex)
                {
                    Console.Error.WriteLine(ex.ToString());
                    string moreToSay =
                        "Unable to retrieve logs from the output helper " +
                        "because of timeout.  No results file can be written.";
                    Console.Error.WriteLine(moreToSay);
                    ret = string.Empty;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.ToString());
                    string moreToSay = "Unable to retrieve logs from the output helper " +
                                       "because of unexpected exception.  No results file can be written.";
                    Console.Error.WriteLine(moreToSay);
                    ret = string.Empty;
                }

                return(ret);
            }