public async Task TestQ2() { var travisUUID = Environment.GetEnvironmentVariable("travis_uuid"); if (travisUUID == null) { travisUUID = ""; } string responseBody = await client.GetStringAsync(baseUrl + "tests/run/2/" + travisUUID); List <TestCase> testCases = JsonConvert.DeserializeObject <List <TestCase> >(responseBody); List <Answer> answers = new List <Answer>(); foreach (var test in testCases) { try { Answer answer = new Answer(); Q2Object input = JsonConvert.DeserializeObject <Q2Object>(test.input); var cancellationToken = new CancellationTokenSource(); cancellationToken.CancelAfter(1000); await Task.Run(() => answer = getSecondAnswer(input, test), cancellationToken.Token); answers.Add(answer); } catch (TaskCanceledException _) { Console.WriteLine("A test in Question 2 has timed out. Tests must complete within one second."); answers.Add(new Answer() { questionNumber = 2, testNumber = test.testNumber, correct = "TIMED_OUT", speed = -1 }); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } } if (travisUUID.Length > 0) { string ans = JsonConvert.SerializeObject(answers); await client.PostAsync(baseUrl + "answer/contestant/" + travisUUID + "/2", new StringContent(ans)); } Assert.IsTrue(answers.All(x => x.correct == "CORRECT")); }
Answer getSecondAnswer(Q2Object input, TestCase test) { var timer = new Stopwatch(); timer.Start(); var answer = Question2.Answer(input.risk, input.bonus, input.trader); timer.Stop(); var timeTaken = ((double)timer.ElapsedTicks / Stopwatch.Frequency) * 1000000000; return(new Answer() { questionNumber = 2, testNumber = test.testNumber, correct = answer == test.output ? "CORRECT" : "INCORRECT", speed = timeTaken }); }