1*8975f5c5SAndroid Build Coastguard Worker#!/usr/bin/env vpython3 2*8975f5c5SAndroid Build Coastguard Worker# 3*8975f5c5SAndroid Build Coastguard Worker# Copyright 2024 The Chromium Authors 4*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be 5*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file. 6*8975f5c5SAndroid Build Coastguard Worker 7*8975f5c5SAndroid Build Coastguard Workerimport unittest 8*8975f5c5SAndroid Build Coastguard Worker 9*8975f5c5SAndroid Build Coastguard Workerfrom unittest import mock 10*8975f5c5SAndroid Build Coastguard Worker 11*8975f5c5SAndroid Build Coastguard Workerimport test_runner 12*8975f5c5SAndroid Build Coastguard Worker 13*8975f5c5SAndroid Build Coastguard Worker 14*8975f5c5SAndroid Build Coastguard Workerclass UploadExceptionTest(unittest.TestCase): 15*8975f5c5SAndroid Build Coastguard Worker 16*8975f5c5SAndroid Build Coastguard Worker def setUp(self): 17*8975f5c5SAndroid Build Coastguard Worker self.sink_client = mock.MagicMock() 18*8975f5c5SAndroid Build Coastguard Worker self.exc_recorder = mock.MagicMock() 19*8975f5c5SAndroid Build Coastguard Worker 20*8975f5c5SAndroid Build Coastguard Worker def testNoExceptions(self): 21*8975f5c5SAndroid Build Coastguard Worker self.exc_recorder.size.return_value = 0 22*8975f5c5SAndroid Build Coastguard Worker test_runner.UploadExceptions(self.sink_client, self.exc_recorder) 23*8975f5c5SAndroid Build Coastguard Worker self.exc_recorder.to_dict.assert_not_called() 24*8975f5c5SAndroid Build Coastguard Worker self.sink_client.UpdateInvocationExtendedProperties.assert_not_called() 25*8975f5c5SAndroid Build Coastguard Worker 26*8975f5c5SAndroid Build Coastguard Worker def testUploadSuccess(self): 27*8975f5c5SAndroid Build Coastguard Worker test_runner.UploadExceptions(self.sink_client, self.exc_recorder) 28*8975f5c5SAndroid Build Coastguard Worker self.exc_recorder.to_dict.assert_called_once() 29*8975f5c5SAndroid Build Coastguard Worker self.sink_client.UpdateInvocationExtendedProperties.assert_called_once() 30*8975f5c5SAndroid Build Coastguard Worker self.exc_recorder.clear.assert_called_once() 31*8975f5c5SAndroid Build Coastguard Worker 32*8975f5c5SAndroid Build Coastguard Worker def testUploadSuccessWithClearStacktrace(self): 33*8975f5c5SAndroid Build Coastguard Worker self.sink_client.UpdateInvocationExtendedProperties.side_effect = [ 34*8975f5c5SAndroid Build Coastguard Worker Exception("Error 1"), None 35*8975f5c5SAndroid Build Coastguard Worker ] 36*8975f5c5SAndroid Build Coastguard Worker test_runner.UploadExceptions(self.sink_client, self.exc_recorder) 37*8975f5c5SAndroid Build Coastguard Worker self.assertEqual(self.exc_recorder.to_dict.call_count, 2) 38*8975f5c5SAndroid Build Coastguard Worker self.assertEqual( 39*8975f5c5SAndroid Build Coastguard Worker self.sink_client.UpdateInvocationExtendedProperties.call_count, 2) 40*8975f5c5SAndroid Build Coastguard Worker self.exc_recorder.clear_stacktrace.assert_called_once() 41*8975f5c5SAndroid Build Coastguard Worker self.exc_recorder.clear.assert_called_once() 42*8975f5c5SAndroid Build Coastguard Worker 43*8975f5c5SAndroid Build Coastguard Worker def testUploadSuccessWithClearRecords(self): 44*8975f5c5SAndroid Build Coastguard Worker self.sink_client.UpdateInvocationExtendedProperties.side_effect = [ 45*8975f5c5SAndroid Build Coastguard Worker Exception("Error 1"), Exception("Error 2"), None 46*8975f5c5SAndroid Build Coastguard Worker ] 47*8975f5c5SAndroid Build Coastguard Worker test_runner.UploadExceptions(self.sink_client, self.exc_recorder) 48*8975f5c5SAndroid Build Coastguard Worker self.assertEqual(self.exc_recorder.to_dict.call_count, 3) 49*8975f5c5SAndroid Build Coastguard Worker self.assertEqual( 50*8975f5c5SAndroid Build Coastguard Worker self.sink_client.UpdateInvocationExtendedProperties.call_count, 3) 51*8975f5c5SAndroid Build Coastguard Worker self.exc_recorder.clear_stacktrace.assert_called_once() 52*8975f5c5SAndroid Build Coastguard Worker self.assertEqual(self.exc_recorder.clear.call_count, 2) 53*8975f5c5SAndroid Build Coastguard Worker self.exc_recorder.register.assert_called_once() 54*8975f5c5SAndroid Build Coastguard Worker 55*8975f5c5SAndroid Build Coastguard Worker def testUploadFailure(self): 56*8975f5c5SAndroid Build Coastguard Worker self.sink_client.UpdateInvocationExtendedProperties.side_effect = ( 57*8975f5c5SAndroid Build Coastguard Worker Exception("Error")) 58*8975f5c5SAndroid Build Coastguard Worker test_runner.UploadExceptions(self.sink_client, self.exc_recorder) 59*8975f5c5SAndroid Build Coastguard Worker self.assertEqual(self.exc_recorder.to_dict.call_count, 3) 60*8975f5c5SAndroid Build Coastguard Worker self.assertEqual( 61*8975f5c5SAndroid Build Coastguard Worker self.sink_client.UpdateInvocationExtendedProperties.call_count, 3) 62*8975f5c5SAndroid Build Coastguard Worker self.assertEqual(self.exc_recorder.clear.call_count, 2) 63*8975f5c5SAndroid Build Coastguard Worker self.exc_recorder.clear_stacktrace.assert_called_once() 64*8975f5c5SAndroid Build Coastguard Worker self.exc_recorder.register.assert_called_once() 65