"""Tests. They want YOU!!"""

import os
import socket
import unittest
import _pylibmc, pylibmc

def get_version(addr):
    (type_, host, port) = addr
    if (type_ != _pylibmc.server_type_tcp):
        raise NotImplementedError("test server can only be on tcp for now")
    else:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            s.connect((host, port))
            s.send("version\r\n")
            version = s.recv(4096)
        finally:
            s.close()
        if not version.startswith("VERSION ") or not version.endswith("\r\n"):
            raise ValueError("unexpected version return: %r" % (version,))
        else:
            version = version[8:-2]
        return version

def is_alive(addr):
    try:
        version = get_version(addr)
    except (ValueError, socket.error):
        version = None
    return (bool(version), version)

def make_test_client(cls=pylibmc.Client, env=None, **kwds):
    """Make a test client.

    >>> make_test_client() is not None
    True
    """
    if env is None:
        env = os.environ

    test_server = (
        _pylibmc.server_type_tcp,
        str(env.get("MEMCACHED_HOST", "127.0.0.1")),
        int(env.get("MEMCACHED_PORT", "11211")))
    
    alive, version = is_alive(test_server)
    assert alive, "test TCP memcached %s not alive" % (test_server[1:],)
    return cls([test_server], **kwds)

class PylibmcTestCase(unittest.TestCase):
    def setUp(self):
        self.mc = make_test_client()

    def tearDown(self):
        self.mc.disconnect_all()
        del self.mc

def dump_infos():
    if hasattr(_pylibmc, "__file__"):
        print "Starting tests with _pylibmc at", _pylibmc.__file__
    else:
        print "Starting tests with static _pylibmc:", _pylibmc
    print "Reported libmemcached version:", _pylibmc.libmemcached_version
    print "Reported pylibmc version:", _pylibmc.__version__
    print "Support compression:", _pylibmc.support_compression

@@ -87,16 +87,414 @@
  pylibmc
+%0Afrom textwrap import dedent%0A%0Aclass NotAliveError(Exception):%0A    template = dedent(%22%22%22%0A    test memcached %25s:%25s not alive%0A%0A    consider setting MEMCACHED_HOST and/or MEMCACHED_PORT%0A    to something more appropriate.%22%22%22).lstrip()%0A%0A    def __init__(self, svr):%0A        (self.server_type, self.address, self.port) = svr%0A%0A    def __str__(self):%0A        return self.template %25 (self.address, self.port)
 %0A%0Adef ge
@@ -1337,307 +1337,611 @@
 ne, 
-**kwds):%0A    %22%22%22Make a test client.%0A%0A    %3E%3E%3E make_test_client() is not None%0A    True%0A    %22%22%22%0A    if env is None:%0A        env = os.environ%0A%0A    test_server = (%0A        _pylibmc.server_type_tcp,%0A        str(env.get(%22MEMCACHED_HOST%22, %22127.0.0.1%22)),%0A        int(env.get(%22MEMCACHED_PORT%22, %2211211%22)))%0A    
+server_type=%22tcp%22,%0A                     host=%22127.0.0.1%22, port=11211, **kwds):%0A    %22%22%22Make a test client. %60env%60 overrides keyword arguments.%0A%0A    %3E%3E%3E make_test_client() is not None%0A    True%0A    %22%22%22%0A    server_types = %7B%22tcp%22: _pylibmc.server_type_tcp,%0A                    %22udp%22: _pylibmc.server_type_udp,%0A                    %22unix%22: _pylibmc.server_type_unix%7D%0A%0A    if env is None:%0A        env = os.environ%0A%0A    styp = env.get(%22MEMCACHED_TYPE%22, server_type)%0A    host = env.get(%22MEMCACHED_HOST%22, host)%0A    port = env.get(%22MEMCACHED_PORT%22, port)%0A%0A    test_server = (server_types%5Bstyp%5D, str(host), int(port))
 %0A   
@@ -1984,97 +1984,103 @@
 er)%0A
+%0A
     
-assert alive, %22test TCP memcached %25s not alive%22 %25 (test_server%5B1:%5D,)%0A    return cls(%5B
+if alive:%0A        return cls(%5Btest_server%5D, **kwds)%0A    else:%0A        raise NotAliveError(
 test
@@ -2086,25 +2086,16 @@
 t_server
-%5D, **kwds
 )%0A%0Aclass
@@ -2139,60 +2139,417 @@
     
-def setUp(self):%0A        self.mc = make_test_client(
+memcached_client = pylibmc.Client%0A    memcached_server_type = %22tcp%22%0A    memcached_host = %22127.0.0.1%22%0A    memcached_port = %2211211%22%0A%0A    def setUp(self):%0A        self.mc = make_test_client(cls=self.memcached_client,%0A                                   server_type=self.memcached_server_type,%0A                                   host=self.memcached_host,%0A                                   port=self.memcached_port
 )%0A%0A 

Improve testing foundation