File: test__core_stat.py

package info (click to toggle)
python-gevent 1.0.1-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,948 kB
  • ctags: 12,954
  • sloc: python: 39,061; ansic: 26,289; sh: 13,582; makefile: 833; awk: 18
file content (59 lines) | stat: -rw-r--r-- 1,689 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from __future__ import with_statement
import gevent
import gevent.core
import os
import time


filename = 'tmp.test__core_stat.%s' % os.getpid()

hub = gevent.get_hub()

DELAY = 0.5

EV_USE_INOTIFY = getattr(gevent.core, 'EV_USE_INOTIFY', None)

try:
    open(filename, 'wb', buffering=0).close()
    assert os.path.exists(filename), filename

    def write():
        f = open(filename, 'wb', buffering=0)
        f.write('x')
        f.close()

    greenlet = gevent.spawn_later(DELAY, write)
    watcher = hub.loop.stat(filename)

    start = time.time()

    with gevent.Timeout(5 + DELAY + 0.5):
        hub.wait(watcher)

    reaction = time.time() - start - DELAY
    print 'Watcher %s reacted after %.4f seconds (write)' % (watcher, reaction)
    if reaction >= DELAY and EV_USE_INOTIFY:
        print 'WARNING: inotify failed (write)'
    assert reaction >= 0.0, 'Watcher %s reacted too early (write): %.3fs' % (watcher, reaction)
    assert watcher.attr is not None, watcher.attr
    assert watcher.prev is not None, watcher.prev

    greenlet.join()
    gevent.spawn_later(DELAY, os.unlink, filename)

    start = time.time()

    with gevent.Timeout(5 + DELAY + 0.5):
        hub.wait(watcher)

    reaction = time.time() - start - DELAY
    print 'Watcher %s reacted after %.4f seconds (unlink)' % (watcher, reaction)
    if reaction >= DELAY and EV_USE_INOTIFY:
        print 'WARNING: inotify failed (unlink)'
    assert reaction >= 0.0, 'Watcher %s reacted too early (unlink): %.3fs' % (watcher, reaction)
    assert watcher.attr is None, watcher.attr
    assert watcher.prev is not None, watcher.prev

finally:
    if os.path.exists(filename):
        os.unlink(filename)