comparison test_ltpda_connection_manager.m @ 0:d5fef23867bb

First workig implementation.
author Daniele Nicolodi <daniele@science.unitn.it>
date Sun, 23 May 2010 10:51:35 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d5fef23867bb
1 function test_ltpda_connection_manager()
2
3 % change with valid credentials
4 host = '127.0.0.1'; % sometimes using localhost causes problems
5 db = 'one';
6 user = 'daniele';
7 passwd = 'daniele';
8
9 % hack user properties
10 p = getappdata(0, 'LTPDApreferences');
11 p.cm.credentialsExpiry = 120; % seconds
12 p.cm.cachePassword = 2; % 0=no 1=yes 2=ask
13 p.cm.maxConnectionsNumber = 10;
14 setappdata(0, 'LTPDApreferences', p);
15
16
17 % reset
18 setappdata(0, 'LTPDAConnectionManager', []);
19 % connection manager
20 cm = LTPDAConnectionManager();
21
22
23 % add credentials
24 n = numel(cm.credentials);
25 cm.add(credentials(host, 'db', 'user', 'passwd'));
26 assert(numel(cm.credentials) == n+1);
27
28 % try again
29 cm.add(credentials(host, 'db', 'user', 'passwd'));
30 % just updated previous entry
31 assert(numel(cm.credentials) == n+1);
32
33 % add other credentials
34 cm.add(credentials('host', 'db'));
35 assert(numel(cm.credentials) == n+2);
36
37 % add user
38 cm.add(credentials('host', 'db', 'user'));
39 assert(numel(cm.credentials) == n+2);
40
41 % add password
42 cm.add(credentials('host', 'db', 'user', 'passwd'));
43 assert(numel(cm.credentials) == n+2);
44
45 % add the oned will be used now on
46 cm.add(credentials(host, db, user, passwd));
47 assert(numel(cm.credentials) == n+3);
48
49
50 % use the credentials cache
51 c = cm.connect(host, db);
52 % one connection in the pool
53 assert(cm.count() == 1);
54 c.close();
55 % still there
56 assert(numel(cm.connections) == 1);
57 n = cm.count();
58 % count has the side effect of removing closed connections from the pool
59 assert(n == 0);
60
61 % open two connections
62 c1 = cm.connect(host, db);
63 c2 = cm.connect(host, db, user);
64 % one connection in the pool
65 assert(numel(cm.connections) == 2);
66 % even when we remove the closed ones
67 assert(cm.count() == 2);
68 % clean up
69 c1.close();
70 c2.close();
71 % no more connections in the pool
72 assert(cm.count() == 0);
73 assert(numel(cm.connections) == 0);
74
75 % plist parameters
76 c = cm.connect(plist('hostname', host, 'database', db));
77 % one more connection in the pool
78 assert(numel(cm.connections) == 1);
79 % clen up
80 c.close();
81
82 % create a connection
83 c1 = cm.connect(host, db);
84 n = cm.count();
85 % specify it as connction parameter
86 c2 = cm.connect(plist('connection', c1));
87 % we should get back the same connection
88 assert(c2 == c1);
89 % it should not have been added to the connection pool
90 assert(numel(cm.connections) == n);
91 % clean up
92 c1.close();
93
94 % no connections in the pool
95 assert(cm.count() == 0);
96 assert(numel(cm.connections) == 0);
97
98 % open a bunch of connections
99 c = cm.connect(host, db);
100 c = cm.connect(host, db);
101 c = cm.connect(host, db);
102 c = cm.connect(host, db);
103 c = cm.connect(host, db);
104 assert(numel(cm.connections) == 5);
105 assert(cm.count() == 5);
106 % close them all
107 cm.close();
108 % check
109 assert(cm.count() == 0);
110
111 end