#!/usr/bin/env python3 """Die Live-Sperre (hand/live.py): eine Live-Sache, toter Halter blockiert nicht.""" import os import sys import tempfile import unittest _w = os.path.dirname(os.path.abspath(__file__)) while _w != "/" and not os.path.isdir(os.path.join(_w, "hand")): _w = os.path.dirname(_w) sys.path.insert(0, os.path.join(_w, "hand")) import live class Sperre(unittest.TestCase): def test_frei_wenn_nichts_da(self): with tempfile.TemporaryDirectory() as d: self.assertIsNone(live.halter(os.path.join(d, "live.json"))) def test_belegen_und_freigeben(self): with tempfile.TemporaryDirectory() as d: p = os.path.join(d, "live.json") ok, txt = live.belegen("laeufer", pid=os.getpid(), pfad=p) self.assertTrue(ok) self.assertIn("laeufer", txt) self.assertEqual(live.halter(p)["pid"], os.getpid()) self.assertTrue(live.freigeben(p)) self.assertIsNone(live.halter(p)) def test_ein_toter_halter_blockiert_nicht(self): with tempfile.TemporaryDirectory() as d: p = os.path.join(d, "live.json") live.belegen("spec", pid=999999, pfad=p) # eine tote pid self.assertIsNone(live.halter(p)) ok, _ = live.belegen("laeufer", pid=os.getpid(), pfad=p) self.assertTrue(ok) def test_ein_lebender_halter_wird_genannt(self): lebend = os.getppid() # lebt auf Mac UND Windows, != eigener pid with tempfile.TemporaryDirectory() as d: p = os.path.join(d, "live.json") live.belegen("laeufer", pid=lebend, pfad=p) self.assertEqual(live.halter(p)["pid"], lebend) ok, txt = live.belegen("spec", pid=os.getpid(), pfad=p) self.assertFalse(ok, "ein lebender Halter muss den Zweiten abweisen") self.assertIn("laeufer", txt) def test_text_ist_lesbar(self): self.assertEqual(live.text(None), "frei") self.assertIn("spec", live.text({"was": "spec", "pid": 1, "seit": 0})) if __name__ == "__main__": unittest.main(verbosity=2)