From 2f9e617932fc8f7103c79091cf0af78485c2e93b Mon Sep 17 00:00:00 2001
From: Piotr Maslanka <piotr.maslanka@henrietta.com.pl>
Date: Sun, 29 Jan 2017 19:10:36 +0100
Subject: [PATCH] reorder for connection

---
 README.md                                |  1 +
 coolamqp/uplink/connection/connection.py | 22 +++++++++++++---------
 2 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/README.md b/README.md
index d63036d..0f94e2c 100644
--- a/README.md
+++ b/README.md
@@ -52,6 +52,7 @@ if you need every CPU cycle you can get.
 
 * v0.89: 
    * Events are no longer timestamped by CoolAMQP, it's your job now
+   * You can delete queues
 
 * v0.88:
     * **API changes:**
diff --git a/coolamqp/uplink/connection/connection.py b/coolamqp/uplink/connection/connection.py
index 2ec98e5..ba65941 100644
--- a/coolamqp/uplink/connection/connection.py
+++ b/coolamqp/uplink/connection/connection.py
@@ -117,13 +117,14 @@ class Connection(object):
         sock.settimeout(0)
         sock.send(b'AMQP\x00\x00\x09\x01')
 
-        Handshaker(self, self.node_definition, self.on_connected)
         self.listener_socket = self.listener_thread.register(sock,
                                                             on_read=self.recvf.put,
                                                             on_fail=self.on_fail)
         self.sendf = SendingFramer(self.listener_socket.send)
         self.watch_for_method(0, (ConnectionClose, ConnectionCloseOk), self.on_connection_close)
 
+        Handshaker(self, self.node_definition, self.on_connected)
+
     def on_fail(self):
         """
         Called by event loop when the underlying connection is closed.
@@ -184,14 +185,17 @@ class Connection(object):
         :param frames: list of frames or None to close the link
         :param reason: optional human-readable reason for this action
         """
-        if frames is not None:
-            # for frame in frames:
-            #     if isinstance(frame, AMQPMethodFrame):
-            #         print('Sending ', frame.payload)
-            self.sendf.send(frames, priority=priority)
-        else:
-            # Listener socket will kill us when time is right
-            self.listener_socket.send(None)
+        try:
+            if frames is not None:
+                # for frame in frames:
+                #     if isinstance(frame, AMQPMethodFrame):
+                #         print('Sending ', frame.payload)
+                self.sendf.send(frames, priority=priority)
+            else:
+                # Listener socket will kill us when time is right
+                self.listener_socket.send(None)
+        except AttributeError:  # .sendf or .listener_socket not there yet
+            raise RuntimeError(u'Call start() first')
 
     def on_frame(self, frame):
         """
-- 
GitLab