File: test_skip_repeats_queue.py

package info (click to toggle)
python-watchdog 6.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 808 kB
  • sloc: python: 6,384; ansic: 609; xml: 155; makefile: 124; sh: 8
file content (80 lines) | stat: -rw-r--r-- 1,712 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import annotations

from watchdog import events
from watchdog.utils.bricks import SkipRepeatsQueue


def test_basic_queue():
    q = SkipRepeatsQueue()

    e1 = (2, "fred")
    e2 = (2, "george")
    e3 = (4, "sally")

    q.put(e1)
    q.put(e2)
    q.put(e3)

    assert e1 == q.get()
    assert e2 == q.get()
    assert e3 == q.get()
    assert q.empty()


def test_allow_nonconsecutive():
    q = SkipRepeatsQueue()

    e1 = (2, "fred")
    e2 = (2, "george")

    q.put(e1)
    q.put(e2)
    q.put(e1)  # repeat the first entry

    assert e1 == q.get()
    assert e2 == q.get()
    assert e1 == q.get()
    assert q.empty()


def test_put_with_watchdog_events():
    # FileSystemEvent.__ne__() uses the key property without
    # doing any type checking. Since _last_item is set to
    # None in __init__(), an AttributeError is raised when
    # FileSystemEvent.__ne__() tries to use None.key
    queue = SkipRepeatsQueue()
    dummy_file = "dummy.txt"
    event = events.FileCreatedEvent(dummy_file)
    queue.put(event)
    assert queue.get() is event


def test_prevent_consecutive():
    q = SkipRepeatsQueue()

    e1 = (2, "fred")
    e2 = (2, "george")

    q.put(e1)
    q.put(e1)  # repeat the first entry (this shouldn't get added)
    q.put(e2)

    assert e1 == q.get()
    assert e2 == q.get()
    assert q.empty()


def test_consecutives_allowed_across_empties():
    q = SkipRepeatsQueue()

    e1 = (2, "fred")

    q.put(e1)
    q.put(e1)  # repeat the first entry (this shouldn't get added)

    assert e1 == q.get()
    assert q.empty()

    q.put(e1)  # this repeat is allowed because 'last' added is now gone from queue
    assert e1 == q.get()
    assert q.empty()