81 lines
4.1 KiB
Python
81 lines
4.1 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from audio_policy import AudioPolicy
|
|
|
|
class Worker:
|
|
def __init__(self):
|
|
import threading
|
|
self.lock=threading.RLock();self.policy='auto';self.job=None;self.process=None;self.warming=False;self.unloaded=0;self.profile_id=None;self.warmed=[]
|
|
def unload_idle(self):self.unloaded+=1;self.process=None;self.profile_id=None
|
|
def warm(self,ident):
|
|
from unittest.mock import Mock
|
|
self.warmed.append(ident);self.profile_id=ident;self.process=Mock();self.process.poll.return_value=None
|
|
|
|
class Profiles:
|
|
def status(self):return {'profiles':[{'id':'tts-1','kind':'audio','runnable':True},{'id':'stt-1','kind':'stt','runnable':True}]}
|
|
|
|
class Endpoint:
|
|
state='stopped'
|
|
|
|
class Scheduler:
|
|
def __init__(self):
|
|
import threading
|
|
self.cv=threading.Condition();self.gpu_mode='llm';self.active=0;self.transition=False;self.queue=[];self.key=('chat','chat-1',1)
|
|
|
|
class PolicyTests(unittest.TestCase):
|
|
def test_persist_validate_and_unload(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path=Path(directory)/'audio-policy.json';tts=Worker();stt=Worker()
|
|
manager=AudioPolicy(path,Profiles(),tts,stt,Scheduler(),Endpoint())
|
|
with self.assertRaises(ValueError):manager.configure({'kind':'tts','mode':'warm','profile_id':'missing'})
|
|
self.assertFalse(path.exists())
|
|
manager.configure({'kind':'tts','mode':'warm','profile_id':'tts-1'})
|
|
self.assertEqual(tts.policy,'warm')
|
|
manager.configure({'kind':'stt','mode':'per_request','profile_id':None})
|
|
self.assertEqual(stt.unloaded,1)
|
|
restored=AudioPolicy(path,Profiles(),Worker(),Worker(),Scheduler(),Endpoint())
|
|
self.assertEqual(restored.status()['settings']['tts']['mode'],'warm')
|
|
self.assertEqual(restored.status()['settings']['stt']['mode'],'per_request')
|
|
|
|
class AutoEndpoint:
|
|
state='running'
|
|
def __init__(self):self.profiles=[{'id':'any-tts','kind':'audio','enabled':True,'runnable':True}]
|
|
def rows(self):return self.profiles
|
|
|
|
class AutomaticPolicyTests(unittest.TestCase):
|
|
def manager(self,directory):
|
|
from unittest.mock import patch
|
|
with patch('audio_policy.threading.Thread.start'):
|
|
return AudioPolicy(Path(directory)/'audio-policy.json',Profiles(),Worker(),Worker(),Scheduler(),AutoEndpoint())
|
|
def test_endpoint_selection_reload_and_no_repeat(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
m=self.manager(directory);m.reconcile();m.reconcile()
|
|
self.assertEqual(m.tts.warmed,['any-tts'])
|
|
m.endpoint.profiles[0]['id']='replacement-tts';m.reconcile()
|
|
self.assertEqual(m.tts.warmed,['any-tts','replacement-tts'])
|
|
self.assertEqual(m.tts.unloaded,1)
|
|
m.endpoint.profiles[0]['enabled']=False;m.reconcile()
|
|
self.assertIsNone(m.tts.process)
|
|
def test_ambiguity_and_unavailable_profile(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
m=self.manager(directory);m.endpoint.profiles.append(dict(m.endpoint.profiles[0],id='second'))
|
|
m.reconcile();self.assertIn('Mehrere',m.errors['tts']);self.assertFalse(m.tts.warmed)
|
|
m.endpoint.profiles.pop();m.endpoint.profiles[0]['runnable']=False
|
|
m.reconcile();self.assertIn('nicht ausführbar',m.errors['tts'])
|
|
def test_no_load_during_other_modes_image_or_transition(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
m=self.manager(directory)
|
|
for mode in ('music','video','voice','separator','restoring'):
|
|
m.scheduler.gpu_mode=mode;m.reconcile()
|
|
m.scheduler.gpu_mode='llm';m.scheduler.key=('image',);m.reconcile()
|
|
m.scheduler.key=('chat','p',1);m.scheduler.transition=True;m.reconcile()
|
|
self.assertFalse(m.tts.warmed)
|
|
def test_per_request_stays_opt_out_and_memory_error_retries(self):
|
|
from unittest.mock import Mock
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
m=self.manager(directory);m.settings['tts']['mode']='per_request';m.reconcile();self.assertFalse(m.tts.warmed)
|
|
m.settings['tts']['mode']='auto';m.tts.warm=Mock(side_effect=ValueError('Zu wenig VRAM'))
|
|
m.reconcile();m.reconcile();self.assertEqual(m.tts.warm.call_count,1);self.assertEqual(m.errors['tts'],'Zu wenig VRAM')
|
|
m.endpoint.profiles[0]['id']='other';m.reconcile();self.assertEqual(m.tts.warm.call_count,2)
|