summaryrefslogtreecommitdiff
path: root/tests/test_gio.py
blob: 3bb27467e0efe362e9a821e847e04b024fe5a811 (plain)
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
# -*- Mode: Python -*-

import os
import unittest

from common import gio, gobject


class TestInputStream(unittest.TestCase):
    def setUp(self):
        f = open("inputstream.txt", "w")
        f.write("testing")

        self._f = open("inputstream.txt", "r")
        self.stream = gio.unix.InputStream(self._f.fileno(), False)

    def tearDown(self):
        self._f.close()
        os.unlink("inputstream.txt")

    def testRead(self):
        self.assertEquals(self.stream.read(), "testing")

    def testReadAsync(self):
        def callback(stream, result):
            read = stream.read_finish(result)
            self.assertEquals(read, len("testing"))
            stream.close()
            loop.quit()

        self.stream.read_async(10240, 0, None, callback)

        loop = gobject.MainLoop()
        loop.run()

    def testReadAsyncError(self):
        self.count = 0
        def callback(stream, result):
            self.count += 1
            if self.count == 1:
                return
            self.assertRaises(gobject.GError, stream.read_finish, result)
            loop.quit()

        self.stream.read_async(10240, 0, None, callback)
        self.stream.read_async(10240, 0, None, callback)

        loop = gobject.MainLoop()
        loop.run()

        self.assertEquals(self.count, 2)


class TestOutputStream(unittest.TestCase):
    def setUp(self):
        self._f = open("outputstream.txt", "w")
        self.stream = gio.unix.OutputStream(self._f.fileno(), False)
        self._f.flush()

    def tearDown(self):
        self._f.close()
        os.unlink("outputstream.txt")

    def testWrite(self):
        self.stream.write("testing")
        self.stream.close()
        self.failUnless(os.path.exists("outputstream.txt"))
        self.assertEquals(open("outputstream.txt").read(), "testing")