Revision 49f986e7

b/test/ganeti.asyncnotifier_unittest.py
41 41
class TestSingleFileEventHandler(testutils.GanetiTestCase):
42 42
  """Test daemon.Mainloop"""
43 43

  
44
  NOTIFIERS = [NOTIFIER_TERM, NOTIFIER_NORM] = range(2)
45

  
44 46
  def setUp(self):
45 47
    testutils.GanetiTestCase.setUp(self)
46 48
    self.mainloop = daemon.Mainloop()
47
    notifier_count = 2
48
    self.chk_files = [self._CreateTempFile() for i in range(notifier_count)]
49
    self.notified = [False for i in range(notifier_count)]
49
    self.chk_files = [self._CreateTempFile() for i in self.NOTIFIERS]
50
    self.notified = [False for i in self.NOTIFIERS]
50 51
    # We need one watch manager per notifier, as those contain the file
51 52
    # descriptor which is monitored by asyncore
52
    self.wms = [pyinotify.WatchManager() for i in range(notifier_count)]
53
    self.cbk = [self.OnInotifyCallback(self.notified, i)
54
                  for i in range(notifier_count)]
53
    self.wms = [pyinotify.WatchManager() for i in self.NOTIFIERS]
54
    self.cbk = [self.OnInotifyCallback(self, i)
55
                 for i in range(len(self.NOTIFIERS))]
55 56
    self.ihandler = [asyncnotifier.SingleFileEventHandler(self.wms[i],
56 57
                                                          self.cbk[i],
57 58
                                                          self.chk_files[i])
58
                      for i in range(notifier_count)]
59
                      for i in range(len(self.NOTIFIERS))]
59 60
    self.notifiers = [asyncnotifier.AsyncNotifier(self.wms[i],
60 61
                                                  self.ihandler[i])
61
                       for i in range(notifier_count)]
62
    # notifier 0 is enabled by default, as we use it to get out of the loop
63
    self.ihandler[0].enable()
62
                       for i in range(len(self.NOTIFIERS))]
63
    # TERM notifier is enabled by default, as we use it to get out of the loop
64
    self.ihandler[self.NOTIFIER_TERM].enable()
64 65

  
65 66
  class OnInotifyCallback:
66
    def __init__(self, notified, i):
67
      self.notified = notified
67
    def __init__(self, testobj, i):
68
      self.testobj = testobj
69
      self.notified = testobj.notified
68 70
      self.i = i
69 71

  
70 72
    def __call__(self, enabled):
71 73
      self.notified[self.i] = True
72
      # notifier 0 is special as we use it to terminate the mainloop
73
      if self.i == 0:
74
      if self.i == self.testobj.NOTIFIER_TERM:
74 75
        os.kill(os.getpid(), signal.SIGTERM)
75 76

  
76 77
  def testReplace(self):
77
    utils.WriteFile(self.chk_files[0], data="dummy")
78
    utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy")
78 79
    self.mainloop.Run()
79
    self.assert_(self.notified[0])
80
    self.assert_(not self.notified[1])
80
    self.assert_(self.notified[self.NOTIFIER_TERM])
81
    self.assert_(not self.notified[self.NOTIFIER_NORM])
81 82

  
82 83
  def testEnableDisable(self):
83
    self.ihandler[0].enable()
84
    self.ihandler[0].disable()
85
    self.ihandler[0].disable()
86
    self.ihandler[0].enable()
87
    self.ihandler[0].disable()
88
    self.ihandler[0].enable()
89
    utils.WriteFile(self.chk_files[0], data="dummy")
84
    self.ihandler[self.NOTIFIER_TERM].enable()
85
    self.ihandler[self.NOTIFIER_TERM].disable()
86
    self.ihandler[self.NOTIFIER_TERM].disable()
87
    self.ihandler[self.NOTIFIER_TERM].enable()
88
    self.ihandler[self.NOTIFIER_TERM].disable()
89
    self.ihandler[self.NOTIFIER_TERM].enable()
90
    utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy")
90 91
    self.mainloop.Run()
91
    self.assert_(self.notified[0])
92
    self.assert_(not self.notified[1])
92
    self.assert_(self.notified[self.NOTIFIER_TERM])
93
    self.assert_(not self.notified[self.NOTIFIER_NORM])
93 94

  
94 95
  def testDoubleEnable(self):
95
    self.ihandler[0].enable()
96
    self.ihandler[0].enable()
97
    utils.WriteFile(self.chk_files[0], data="dummy")
96
    self.ihandler[self.NOTIFIER_TERM].enable()
97
    self.ihandler[self.NOTIFIER_TERM].enable()
98
    utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy")
98 99
    self.mainloop.Run()
99
    self.assert_(self.notified[0])
100
    self.assert_(not self.notified[1])
100
    self.assert_(self.notified[self.NOTIFIER_TERM])
101
    self.assert_(not self.notified[self.NOTIFIER_NORM])
101 102

  
102 103
  def testDefaultDisabled(self):
103
    utils.WriteFile(self.chk_files[1], data="dummy")
104
    utils.WriteFile(self.chk_files[0], data="dummy")
104
    utils.WriteFile(self.chk_files[self.NOTIFIER_NORM], data="dummy")
105
    utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy")
105 106
    self.mainloop.Run()
106
    self.assert_(self.notified[0])
107
    # notifier 1 is disabled by default
108
    self.assert_(not self.notified[1])
107
    self.assert_(self.notified[self.NOTIFIER_TERM])
108
    # NORM notifier is disabled by default
109
    self.assert_(not self.notified[self.NOTIFIER_NORM])
109 110

  
110 111
  def testBothEnabled(self):
111
    self.ihandler[1].enable()
112
    utils.WriteFile(self.chk_files[1], data="dummy")
113
    utils.WriteFile(self.chk_files[0], data="dummy")
112
    self.ihandler[self.NOTIFIER_NORM].enable()
113
    utils.WriteFile(self.chk_files[self.NOTIFIER_NORM], data="dummy")
114
    utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy")
114 115
    self.mainloop.Run()
115
    self.assert_(self.notified[0])
116
    self.assert_(self.notified[1])
116
    self.assert_(self.notified[self.NOTIFIER_TERM])
117
    self.assert_(self.notified[self.NOTIFIER_NORM])
117 118

  
118 119

  
119 120
if __name__ == "__main__":

Also available in: Unified diff