view test_ltpda_connection_manager.m @ 3:b4f2f4c10918

Add help comments.
author Daniele Nicolodi <daniele@science.unitn.it>
date Sun, 23 May 2010 22:12:05 +0200
parents d5fef23867bb
children
line wrap: on
line source

function test_ltpda_connection_manager()

  % change with valid credentials
  host   = '127.0.0.1'; % sometimes using localhost causes problems
  db     = 'one';
  user   = 'daniele';
  passwd = 'daniele';

  % hack user properties
  p = getappdata(0, 'LTPDApreferences');
  p.cm.credentialsExpiry = 120; % seconds
  p.cm.cachePassword = 2; % 0=no 1=yes 2=ask
  p.cm.maxConnectionsNumber = 10;
  setappdata(0, 'LTPDApreferences', p);


  % reset
  setappdata(0, 'LTPDAConnectionManager', []);
  % connection manager
  cm = LTPDAConnectionManager();


  % add credentials
  n = numel(cm.credentials);
  cm.add(credentials(host, 'db', 'user', 'passwd'));
  assert(numel(cm.credentials) == n+1);

  % try again
  cm.add(credentials(host, 'db', 'user', 'passwd'));
  % just updated previous entry
  assert(numel(cm.credentials) == n+1);

  % add other credentials
  cm.add(credentials('host', 'db'));
  assert(numel(cm.credentials) == n+2);

  % add user
  cm.add(credentials('host', 'db', 'user'));
  assert(numel(cm.credentials) == n+2);

  % add  password
  cm.add(credentials('host', 'db', 'user', 'passwd'));
  assert(numel(cm.credentials) == n+2);

  % add the oned will be used now on
  cm.add(credentials(host, db, user, passwd));
  assert(numel(cm.credentials) == n+3);


  % use the credentials cache
  c = cm.connect(host, db);
  % one connection in the pool
  assert(cm.count() == 1);
  c.close();
  % still there
  assert(numel(cm.connections) == 1);
  n = cm.count();
  % count has the side effect of removing closed connections from the pool
  assert(n == 0);

  % open two connections
  c1 = cm.connect(host, db);
  c2 = cm.connect(host, db, user);
  % one connection in the pool
  assert(numel(cm.connections) == 2);
  % even when we remove the closed ones
  assert(cm.count() == 2);
  % clean up
  c1.close();
  c2.close();
  % no more connections in the pool
  assert(cm.count() == 0);
  assert(numel(cm.connections) == 0);

  % plist parameters
  c = cm.connect(plist('hostname', host, 'database', db));
  % one more connection in the pool
  assert(numel(cm.connections) == 1);
  % clen up
  c.close();

  % create a connection
  c1 = cm.connect(host, db);
  n = cm.count();
  % specify it as connction parameter
  c2 = cm.connect(plist('connection', c1));
  % we should get back the same connection
  assert(c2 == c1);
  % it should not have been added to the connection pool
  assert(numel(cm.connections) == n);
  % clean up
  c1.close();

  % no connections in the pool
  assert(cm.count() == 0);
  assert(numel(cm.connections) == 0);

  % open a bunch of connections
  c = cm.connect(host, db);
  c = cm.connect(host, db);
  c = cm.connect(host, db);
  c = cm.connect(host, db);
  c = cm.connect(host, db);
  assert(numel(cm.connections) == 5);
  assert(cm.count() == 5);
  % close them all
  cm.close();
  % check
  assert(cm.count() == 0);

end