alerts: handle min duration properly (#23191)
	
		
	
				
					
				
			* alerts: handle min duration properly
* add active
* tests
* cleanup test
* update refs
old-commit-hash: 07b971d473
			
			
				commatwo_master
			
			
		
							parent
							
								
									853dc0d016
								
							
						
					
					
						commit
						700ad9ec50
					
				
				 5 changed files with 62 additions and 14 deletions
			
			
		| @ -0,0 +1,45 @@ | |||||||
|  | #!/usr/bin/env python3 | ||||||
|  | import random | ||||||
|  | import unittest | ||||||
|  | 
 | ||||||
|  | from selfdrive.controls.lib.events import Alert, EVENTS | ||||||
|  | from selfdrive.controls.lib.alertmanager import AlertManager | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class TestAlertManager(unittest.TestCase): | ||||||
|  | 
 | ||||||
|  |   def test_duration(self): | ||||||
|  |     """ | ||||||
|  |       Enforce that an alert lasts for max(alert duration, duration the alert is added) | ||||||
|  |     """ | ||||||
|  |     for duration in range(1, 100): | ||||||
|  |       alert = None | ||||||
|  |       while not isinstance(alert, Alert): | ||||||
|  |         event = random.choice([e for e in EVENTS.values() if len(e)]) | ||||||
|  |         alert = random.choice(list(event.values())) | ||||||
|  | 
 | ||||||
|  |       alert.duration = duration | ||||||
|  | 
 | ||||||
|  |       # check two cases: | ||||||
|  |       # - alert is added to AM for <= the alert's duration | ||||||
|  |       # - alert is added to AM for > alert's duration | ||||||
|  |       for greater in (True, False): | ||||||
|  |         if greater: | ||||||
|  |           add_duration = duration + random.randint(1, 10) | ||||||
|  |         else: | ||||||
|  |           add_duration = random.randint(1, duration) | ||||||
|  |         show_duration = max(duration, add_duration) | ||||||
|  | 
 | ||||||
|  |         AM = AlertManager() | ||||||
|  |         for frame in range(duration+10): | ||||||
|  |           if frame < add_duration: | ||||||
|  |             AM.add_many(frame, [alert, ]) | ||||||
|  |           AM.process_alerts(frame) | ||||||
|  | 
 | ||||||
|  |           shown = AM.alert is not None | ||||||
|  |           should_show = frame <= show_duration | ||||||
|  |           self.assertEqual(shown, should_show, msg=f"{frame=} {add_duration=} {duration=}") | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | if __name__ == "__main__": | ||||||
|  |   unittest.main() | ||||||
| @ -1 +1 @@ | |||||||
| c09fc7b1409529a9991428845ee14f0e37d95b2d | 0ae46ae318a63476d8905aa0c32b0e587177868a | ||||||
					Loading…
					
					
				
		Reference in new issue