instrumentation: add next-share/
[cs-p2p-next.git] / instrumentation / next-share / BaseLib / Test / test_seeding_stats.py
1 # Written by Boudewijn Schoon
2 # see LICENSE.txt for license information
3
4 import sys
5 import unittest
6 import cPickle
7
8 from BaseLib.Core.BitTornado.BT1.MessageID import *
9 from BaseLib.Core.CacheDB.SqliteCacheDBHandler import CrawlerDBHandler
10 from BaseLib.Core.CacheDB.SqliteSeedingStatsCacheDB import *
11
12 from olconn import OLConnection
13 from test_crawler import TestCrawler
14
15 DEBUG=True
16
17 class TestSeedingStats(TestCrawler):
18     """ 
19     Testing Seeding-Stats statistics gathering using the Crawler framework
20     """
21
22     def test_all(self):
23         """
24         I want to start a Tribler client once and then connect to it
25         many times. So there must be only one test method to prevent
26         setUp() from creating a new client every time.
27
28         The code is constructed so unittest will show the name of the
29         (sub)test where the error occured in the traceback it prints.
30         """
31         self.subtest_invalid_query()
32         self.subtest_valid_query()
33
34     def subtest_invalid_query(self):
35         """
36         Send a CRAWLER_SEEDINGSTATS_QUERY message to the Tribler
37         instance. Execute an invalid SQL query.
38         """
39         print >>sys.stderr, "-"*80, "\ntest: subtest_invalid_query"
40
41         # make sure that the OLConnection IS in the crawler_db
42         crawler_db = CrawlerDBHandler.getInstance()
43         crawler_db.temporarilyAddCrawler(self.my_permid)
44
45         s = OLConnection(self.my_keypair, "localhost", self.hisport)
46
47         queries = ["FOO BAR", cPickle.dumps(["select * from category", ""])]
48         for query in queries:
49             self.send_crawler_request(s, CRAWLER_SEEDINGSTATS_QUERY, 0, 0, query)
50
51             error, payload = self.receive_crawler_reply(s, CRAWLER_SEEDINGSTATS_QUERY, 0)
52             assert error != 0, error
53             if DEBUG:
54                 print >>sys.stderr, "test_seeding_stats:", payload
55
56 #        time.sleep(1)
57         
58     def subtest_valid_query(self):
59         """
60         Send a CRAWLER_SEEDINGSTATS_QUERY message to the Tribler
61         instance. Execute a valid SQL query.
62         """
63         print >>sys.stderr, "-"*80, "\ntest: subtest_valid_query"
64
65         # make sure that the OLConnection IS in the crawler_db
66         crawler_db = CrawlerDBHandler.getInstance()
67         crawler_db.temporarilyAddCrawler(self.my_permid)
68         
69         # test with valid data
70         seedingstats_db = SQLiteSeedingStatsCacheDB.getInstance()
71         seedingstats_db.insertMany("SeedingStats", [(50000, 'foobar', 'dummy_seed', 500, 0, 0), (80000, 'bar', 'dummy_seed', 800, 1, 0)])
72         
73         s = OLConnection(self.my_keypair, "localhost", self.hisport, mylistenport=self.listen_port)
74
75         queries = [cPickle.dumps([("read", "SELECT * FROM SeedingStats"), ("read", "SELECT * FROM SeedingStats WHERE crawled = 0")])]
76         for query in queries:
77             self.send_crawler_request(s, CRAWLER_SEEDINGSTATS_QUERY, 0, 0, query)
78
79             error, payload = self.receive_crawler_reply(s, CRAWLER_SEEDINGSTATS_QUERY, 0)
80             assert error == 0, (error, payload)
81
82             if DEBUG:
83                 print >>sys.stderr, "test_seeding_stats:", cPickle.loads(payload)
84
85 #        time.sleep(1)
86
87 if __name__ == "__main__":
88     def test_suite():
89         suite = unittest.TestSuite()
90         suite.addTest(unittest.makeSuite(TestSeedingStats))
91         return suite
92     unittest.main(defaultTest="test_suite")
93