~ubuntu-branches/debian/jessie/gamin/jessie

« back to all changes in this revision

Viewing changes to python/tests/flood.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2007-03-23 14:43:49 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070323144349-1inpdk22uaneks9h
Tags: 0.1.8-2
* debian/control: Improve long description. (Closes: #405347)
* debian/patches/14_nfs-fix.patch: Fix gam_server startup for Thunar.
  Thanks to Maximiliano Curia. (Closes: #403247)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Check the flood control under dnotify
 
4
# Scenario: the main thread watch a file, a second thread is created
 
5
#  writing 10 times a sec to that file, then after 5 seconds it stops
 
6
#  and stops modifying the file for 6 seconds.
 
7
#  the kernel monitoring should be stopped within one second in the
 
8
#  first phase and then reactivated after a few seconds in the second
 
9
#  phase.
 
10
import shutil
 
11
import os
 
12
import sys
 
13
import signal
 
14
import gamin
 
15
import time
 
16
import thread
 
17
 
 
18
threads = 0
 
19
 
 
20
# create a 4k block
 
21
block = '1234'
 
22
i = 0
 
23
while i < 10:
 
24
    block = block + block
 
25
    i = i + 1
 
26
 
 
27
def wait_ms(ms = 100):
 
28
    delay = 0.001
 
29
    delay = delay * ms
 
30
    time.sleep(delay)
 
31
 
 
32
def resource_update_thread(filename):
 
33
    global threads
 
34
    global block
 
35
 
 
36
    threads = threads + 1
 
37
#    print "%s active" % (filename)
 
38
    f = open(filename, "w")
 
39
    f.write(block)
 
40
    i = 0
 
41
    while i < 50:
 
42
        wait_ms(100)
 
43
        i = i + 1
 
44
        f.write(block)
 
45
#    print "%s quiet" % (filename)
 
46
    wait_ms(10000)
 
47
    f.write(block)
 
48
    f.close()
 
49
    wait_ms(1000)
 
50
    threads = threads - 1
 
51
    thread.exit()
 
52
 
 
53
ok = 1
 
54
top = 0
 
55
dbg = 0
 
56
db_expect = [ 51, 54, 55, 52 ]
 
57
expect = [gamin.GAMExists, gamin.GAMEndExist]
 
58
 
 
59
def debug(path, type, data):
 
60
    global dbg, db_expect, ok
 
61
 
 
62
#    print "Got debug %s, %s, %s" % (path, type, data)
 
63
    if path[-8:] != "temp_dir":
 
64
        print "Error got debug path unexpected %s" % (path)
 
65
        ok = 0
 
66
    if db_expect[dbg] != type:
 
67
        print "Error got debug event %d expected %d" % (type, db_expect[dbg])
 
68
        ok = 0
 
69
    dbg = dbg + 1
 
70
 
 
71
def callback(path, event, which):
 
72
    global top, expect, ok
 
73
    print "Got callback: %s, %s" % (path, event)
 
74
    if event == gamin.GAMAcknowledge:
 
75
        return
 
76
    if top < 2:
 
77
        if expect[top] != event:
 
78
            print "Error got event %d expected %d" % (event, expect[top])
 
79
            ok = 0
 
80
    elif event != gamin.GAMChanged:
 
81
        print "Error got event %d expected %d" % (event, gamin.GAMChanged)
 
82
        ok = 0
 
83
    top = top + 1
 
84
 
 
85
shutil.rmtree ("temp_dir", True)
 
86
os.mkdir ("temp_dir")
 
87
f = open("temp_dir/a", "w").close()
 
88
thread.start_new_thread(resource_update_thread, ("temp_dir/a",))
 
89
 
 
90
mon = gamin.WatchMonitor()
 
91
mon._debug_object("notify", debug, 0)
 
92
mon.watch_file("temp_dir/a", callback, 0)
 
93
 
 
94
# wait until the thread finishes, collecting events
 
95
wait_ms(100)
 
96
while threads > 0:
 
97
    mon.handle_events()
 
98
    wait_ms(100)
 
99
 
 
100
#print "all threads terminated, exiting ..."
 
101
 
 
102
mon.stop_watch("temp_dir/a")
 
103
time.sleep(1)
 
104
mon.handle_events()
 
105
mon.disconnect()
 
106
del mon
 
107
shutil.rmtree ("temp_dir", True)
 
108
 
 
109
if top <= 2:
 
110
    print "Error: monitor got only %d events" % (top)
 
111
elif top >= 15:
 
112
    print "Error: event flow didn't worked properly, gor %d events" % (top)
 
113
elif dbg != 4 and gamin.has_debug_api == 1:
 
114
    print "Error: debug got %d events insteads of 4" % (dbg)
 
115
elif ok == 1:
 
116
    print "OK"