1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128

content / test / gpu / bad_machine_finder / tasks_unittest.py [blame]

# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest

from bad_machine_finder import tasks


class BotStatsUnittest(unittest.TestCase):

  def testInputValidation(self):
    """Tests that inputs are properly validated."""
    bot_stats = tasks.BotStats()
    with self.assertRaisesRegex(ValueError, 'total_tasks must be positive'):
      bot_stats.AddStatsForSuite('suite', 0, 0)
    with self.assertRaisesRegex(ValueError,
                                'failed_tasks must be non-negative'):
      bot_stats.AddStatsForSuite('suite', 1, -1)
    with self.assertRaisesRegex(ValueError,
                                'total_tasks must be >= failed_tasks'):
      bot_stats.AddStatsForSuite('suite', 5, 10)
    bot_stats.AddStatsForSuite('suite_name', 10, 5)
    with self.assertRaisesRegex(
        ValueError, 'Stats for test suite suite_name were already provided .*'):
      bot_stats.AddStatsForSuite('suite_name', 5, 0)

  def testOnlyReadableWhenFrozen(self):
    """Tests that data is only readable once the object is frozen."""
    bot_stats = tasks.BotStats()
    with self.assertRaises(AssertionError):
      _ = bot_stats.total_tasks
    with self.assertRaises(AssertionError):
      _ = bot_stats.failed_tasks
    with self.assertRaises(AssertionError):
      _ = bot_stats.overall_failure_rate
    with self.assertRaises(AssertionError):
      bot_stats.GetTotalTasksForSuite('suite')
    with self.assertRaises(AssertionError):
      bot_stats.GetFailedTasksForSuite('suite')

  def testOnlyWritableWhenNotFrozen(self):
    """Tests that data is only writable when the object is not frozen."""
    bot_stats = tasks.BotStats()
    bot_stats.Freeze()
    with self.assertRaises(AssertionError):
      bot_stats.AddStatsForSuite('suite', 10, 5)

  def testStatTracking(self):
    """Tests that the tracked stats are correct."""
    bot_stats = tasks.BotStats()
    bot_stats.AddStatsForSuite('pixel', 10, 5)
    bot_stats.AddStatsForSuite('webgl', 20, 0)
    bot_stats.Freeze()

    self.assertEqual(bot_stats.total_tasks, 30)
    self.assertEqual(bot_stats.failed_tasks, 5)
    self.assertEqual(bot_stats.overall_failure_rate, float(5) / 30)
    self.assertEqual(bot_stats.GetTotalTasksForSuite('pixel'), 10)
    self.assertEqual(bot_stats.GetFailedTasksForSuite('pixel'), 5)
    self.assertEqual(bot_stats.GetTotalTasksForSuite('webgl'), 20)
    self.assertEqual(bot_stats.GetFailedTasksForSuite('webgl'), 0)
    self.assertEqual(bot_stats.GetTotalTasksForSuite('non-existent'), 0)
    self.assertEqual(bot_stats.GetFailedTasksForSuite('non-existent'), 0)


class MixinStatsUnittest(unittest.TestCase):

  def testInputValidation(self):
    """Tests that inputs are properly validated."""
    mixin_stats = tasks.MixinStats()
    with self.assertRaisesRegex(ValueError, 'total_tasks must be positive'):
      mixin_stats.AddStatsForBotAndSuite('bot', 'suite', 0, 0)
    with self.assertRaisesRegex(ValueError,
                                'failed_tasks must be non-negative'):
      mixin_stats.AddStatsForBotAndSuite('bot', 'suite', 10, -1)

  def testOnlyReadableWhenFrozen(self):
    """Tests that data is only readable once the object is frozen."""
    mixin_stats = tasks.MixinStats()
    with self.assertRaises(AssertionError):
      _ = mixin_stats.total_tasks
    with self.assertRaises(AssertionError):
      _ = mixin_stats.failed_tasks
    with self.assertRaises(AssertionError):
      list(mixin_stats.IterBots())
    with self.assertRaises(AssertionError):
      mixin_stats.GetOverallFailureRates()

  def testOnlyWritableWhenNotFrozen(self):
    """Tests that data is only writable when the object is not frozen."""
    mixin_stats = tasks.MixinStats()
    mixin_stats.Freeze()
    with self.assertRaises(AssertionError):
      mixin_stats.AddStatsForBotAndSuite('bot', 'suite', 10, 0)

  def testStatTracking(self):
    """Tests that the tracked stats are correct."""
    mixin_stats = tasks.MixinStats()
    mixin_stats.AddStatsForBotAndSuite('bot-1', 'suite-1', 10, 0)
    mixin_stats.AddStatsForBotAndSuite('bot-1', 'suite-2', 20, 5)
    mixin_stats.AddStatsForBotAndSuite('bot-2', 'suite-1', 30, 30)
    mixin_stats.AddStatsForBotAndSuite('bot-2', 'suite-2', 10, 5)
    mixin_stats.Freeze()

    self.assertEqual(mixin_stats.total_tasks, 70)
    self.assertEqual(mixin_stats.failed_tasks, 40)
    failure_rates = mixin_stats.GetOverallFailureRates()
    self.assertEqual(len(failure_rates), 2)
    self.assertEqual(set(failure_rates), {float(5) / 30, float(35) / 40})
    for bot_id, bot_stats in mixin_stats.IterBots():
      self.assertIn(bot_id, ('bot-1', 'bot-2'))
      if bot_id == 'bot-1':
        self.assertEqual(bot_stats.total_tasks, 30)
        self.assertEqual(bot_stats.failed_tasks, 5)
        self.assertEqual(bot_stats.overall_failure_rate, float(5) / 30)
        self.assertEqual(bot_stats.GetTotalTasksForSuite('suite-1'), 10)
        self.assertEqual(bot_stats.GetFailedTasksForSuite('suite-1'), 0)
        self.assertEqual(bot_stats.GetTotalTasksForSuite('suite-2'), 20)
        self.assertEqual(bot_stats.GetFailedTasksForSuite('suite-2'), 5)
      else:
        self.assertEqual(bot_stats.total_tasks, 40)
        self.assertEqual(bot_stats.failed_tasks, 35)
        self.assertEqual(bot_stats.overall_failure_rate, float(35) / 40)
        self.assertEqual(bot_stats.GetTotalTasksForSuite('suite-1'), 30)
        self.assertEqual(bot_stats.GetFailedTasksForSuite('suite-1'), 30)
        self.assertEqual(bot_stats.GetTotalTasksForSuite('suite-2'), 10)
        self.assertEqual(bot_stats.GetFailedTasksForSuite('suite-2'), 5)