'test the MessageTracker object for tracking when zmq is done with a buffer'

@skip_pypy
def test_tracker(self):

    addr = 'tcp://127.0.0.1'
    sock = socket.socket()
    sock.bind(('127.0.0.1', 0))
    port = sock.getsockname()[1]
    iface = ('%s:%i' % (addr, port))
    sock.close()
    time.sleep(0.1)
    a = self.context.socket(zmq.PUSH)
    b = self.context.socket(zmq.PULL)
    self.sockets.extend([a, b])
    a.connect(iface)
    time.sleep(0.1)
    p1 = a.send('something', copy=False, track=True)
    assert isinstance(p1, zmq.MessageTracker)
    assert (p1 is zmq._FINISHED_TRACKER)
    assert p1.done
    a.copy_threshold = 0
    p2 = a.send_multipart(['something', 'else'], copy=False, track=True)
    assert isinstance(p2, zmq.MessageTracker)
    assert (not p2.done)
    b.bind(iface)
    msg = self.recv_multipart(b)
    for i in range(10):
        if p1.done:
            break
        time.sleep(0.1)
    self.assertEqual(p1.done, True)
    self.assertEqual(msg, ['something'])
    msg = self.recv_multipart(b)
    for i in range(10):
        if p2.done:
            break
        time.sleep(0.1)
    self.assertEqual(p2.done, True)
    self.assertEqual(msg, ['something', 'else'])
    m = zmq.Frame('again', track=True)
    self.assertEqual(m.tracker.done, False)
    p1 = a.send(m, copy=False)
    p2 = a.send(m, copy=False)
    self.assertEqual(m.tracker.done, False)
    self.assertEqual(p1.done, False)
    self.assertEqual(p2.done, False)
    msg = self.recv_multipart(b)
    self.assertEqual(m.tracker.done, False)
    self.assertEqual(msg, ['again'])
    msg = self.recv_multipart(b)
    self.assertEqual(m.tracker.done, False)
    self.assertEqual(msg, ['again'])
    self.assertEqual(p1.done, False)
    self.assertEqual(p2.done, False)
    pm = m.tracker
    del m
    for i in range(10):
        if p1.done:
            break
        time.sleep(0.1)
    self.assertEqual(p1.done, True)
    self.assertEqual(p2.done, True)
    m = zmq.Frame('something', track=False)
    self.assertRaises(ValueError, a.send, m, copy=False, track=True)
