~smoser/ubuntu/lucid/python-boto/debian-1.9b-merge

« back to all changes in this revision

Viewing changes to tests/test_sqsconnection.py

  • Committer: Bazaar Package Importer
  • Author(s): Eric Evans
  • Date: 2007-07-16 17:17:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070716171748-bsw9rlyu0yuui9lb
Tags: upstream-0.9b
ImportĀ upstreamĀ versionĀ 0.9b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
 
4
#
 
5
# Permission is hereby granted, free of charge, to any person obtaining a
 
6
# copy of this software and associated documentation files (the
 
7
# "Software"), to deal in the Software without restriction, including
 
8
# without limitation the rights to use, copy, modify, merge, publish, dis-
 
9
# tribute, sublicense, and/or sell copies of the Software, and to permit
 
10
# persons to whom the Software is furnished to do so, subject to the fol-
 
11
# lowing conditions:
 
12
#
 
13
# The above copyright notice and this permission notice shall be included
 
14
# in all copies or substantial portions of the Software.
 
15
#
 
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
17
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 
18
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
 
19
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 
20
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
21
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 
22
# IN THE SOFTWARE.
 
23
 
 
24
"""
 
25
Some unit tests for the SQSConnection
 
26
"""
 
27
 
 
28
import unittest
 
29
import time
 
30
from boto.sqs.connection import SQSConnection
 
31
from boto.exception import SQSError
 
32
 
 
33
class SQSConnectionTest (unittest.TestCase):
 
34
 
 
35
    def test_1_basic(self):
 
36
        print '--- running SQSConnection tests ---'
 
37
        c = SQSConnection()
 
38
        rs = c.get_all_queues()
 
39
        num_queues = 0
 
40
        for q in rs:
 
41
            num_queues += 1
 
42
    
 
43
        # try illegal name
 
44
        try:
 
45
            queue = c.create_queue('bad_queue_name')
 
46
        except SQSError:
 
47
            pass
 
48
        
 
49
        # now create one that should work and should be unique (i.e. a new one)
 
50
        queue_name = 'test%d' % int(time.time())
 
51
        timeout = 60
 
52
        queue = c.create_queue(queue_name, timeout)
 
53
        time.sleep(10)
 
54
        rs  = c.get_all_queues()
 
55
        i = 0
 
56
        for q in rs:
 
57
            i += 1
 
58
        assert i == num_queues+1
 
59
        assert queue.count_slow() == 0
 
60
 
 
61
        # check the visibility timeout
 
62
        t = queue.get_timeout()
 
63
        assert t == timeout, '%d != %d' % (t, timeout)
 
64
 
 
65
        # now try to get queue attributes
 
66
        a = q.get_attributes()
 
67
        assert a.has_key('ApproximateNumberOfMessages')
 
68
        assert a.has_key('VisibilityTimeout')
 
69
        a = q.get_attributes('ApproximateNumberOfMessages')
 
70
        assert a.has_key('ApproximateNumberOfMessages')
 
71
        assert not a.has_key('VisibilityTimeout')
 
72
        a = q.get_attributes('VisibilityTimeout')
 
73
        assert not a.has_key('ApproximateNumberOfMessages')
 
74
        assert a.has_key('VisibilityTimeout')
 
75
 
 
76
        # now change the visibility timeout
 
77
        timeout = 45
 
78
        queue.set_timeout(timeout)
 
79
        t = queue.get_timeout()
 
80
        assert t == timeout, '%d != %d' % (t, timeout)
 
81
    
 
82
        # now add a message
 
83
        message_body = 'This is a test\n'
 
84
        message = queue.new_message(message_body)
 
85
        queue.write(message)
 
86
        time.sleep(5)
 
87
        assert queue.count_slow() == 1
 
88
        time.sleep(10)
 
89
 
 
90
        # now read the message from the queue with a 10 second timeout
 
91
        message = queue.read(visibility_timeout=10)
 
92
        assert message
 
93
        assert message.get_body() == message_body
 
94
 
 
95
        # now immediately try another read, shouldn't find anything
 
96
        message = queue.read()
 
97
        assert message == None
 
98
 
 
99
        # now wait 10 seconds and try again
 
100
        time.sleep(10)
 
101
        message = queue.read()
 
102
        assert message
 
103
 
 
104
        # now terminate the visibility timeout for this message
 
105
        message.change_visibility(0)
 
106
 
 
107
        # now see if we can read it in the queue
 
108
        message = queue.read()
 
109
        assert message
 
110
 
 
111
        # now delete the message
 
112
        queue.delete_message(message)
 
113
        time.sleep(5)
 
114
        assert queue.count_slow() == 0
 
115
 
 
116
        # now delete that queue
 
117
        c.delete_queue(queue)
 
118
        rs = c.get_all_queues()
 
119
        i = 0
 
120
        for q in rs:
 
121
            i += 1
 
122
        assert i == num_queues
 
123
 
 
124
        print '--- tests completed ---'
 
125