Fix timing issue with bundle downloads
diff --git a/api.go b/api.go
index 9ce3bea..09a4d17 100644
--- a/api.go
+++ b/api.go
@@ -68,7 +68,7 @@
 				}
 			}
 		case subscriber := <-addSubscriber:
-			log.Debugf("Add subscriber: %s", subscriber)
+			log.Debugf("Add subscriber: %v", subscriber)
 			subscribers[subscriber] = struct{}{}
 		}
 	}
diff --git a/api_test.go b/api_test.go
index 1076ff8..669f3e0 100644
--- a/api_test.go
+++ b/api_test.go
@@ -127,12 +127,20 @@
 
 				Expect(depRes.DeploymentID).Should(Equal(deploymentID))
 
+				for _, bundle := range depRes.Bundles {
+					uri, err := url.Parse(bundle.URI)
+					Expect(err).ShouldNot(HaveOccurred())
+					bContent, err := ioutil.ReadFile(uri.Path)
+					Expect(err).ShouldNot(HaveOccurred())
+					content := string(bContent)
+					Expect(content).Should(HavePrefix("/bundle/"))
+				}
+
 				close(done)
 			}()
 
-			time.Sleep(50 * time.Millisecond) // make sure API call is made and blocks
-			insertTestDeployment(testServer, deploymentID)
-			incoming <- deploymentID
+			time.Sleep(25 * time.Millisecond) // give api call above time to block
+			triggerDeploymentEvent(deploymentID)
 		})
 
 		It("should get 304 after blocking if no new deployment", func() {
diff --git a/data.go b/data.go
index 45d2cc1..0fd46b9 100644
--- a/data.go
+++ b/data.go
@@ -118,8 +118,6 @@
 	}
 
 	log.Debugf("INSERT gateway_deploy_deployment %s succeeded", depID)
-
-	incoming <- depID
 	return err
 }
 
@@ -191,6 +189,11 @@
 	}
 
 	log.Debugf("UPDATE gateway_deploy_deployment %s succeeded", depID)
+
+	if status == DEPLOYMENT_STATE_READY {
+		incoming<- depID
+	}
+
 	return nil
 }
 
diff --git a/deployments.go b/deployments.go
index ea7c81a..8fe22f4 100644
--- a/deployments.go
+++ b/deployments.go
@@ -99,7 +99,7 @@
 		return nil, err
 	}
 	if res.StatusCode != 200 {
-		return nil, fmt.Errorf("Bundle uri %s failed with status %s", uriString, res.StatusCode)
+		return nil, fmt.Errorf("Bundle uri %s failed with status %d", uriString, res.StatusCode)
 	}
 	return res.Body, nil
 }
@@ -120,6 +120,7 @@
 			log.Errorf("Unable to create temp file: %v", err)
 			return
 		}
+		defer tempFile.Close()
 		fileName = tempFile.Name()
 
 		var bundleReader io.ReadCloser
@@ -136,11 +137,7 @@
 			return
 		}
 
-		err = tempFile.Close()
-		if err != nil {
-			log.Errorf("Unable to close file %s: %v", tempFile, err)
-		}
-
+		log.Debugf("Bundle downloaded: %s", bun.URI)
 		return
 	}
 
@@ -210,7 +207,7 @@
 		bun := dep.Bundles[i]
 		go func() {
 			err := prepareBundle(depID, bun)
-			errorsChan <- err
+			errorsChan<- err
 			if err != nil {
 				id := string(i)
 				err = updateBundleStatus(db, depID, id, DEPLOYMENT_STATE_ERR_APID, ERROR_CODE_TODO, err.Error())
diff --git a/listener.go b/listener.go
index 915a6b5..39e35c2 100644
--- a/listener.go
+++ b/listener.go
@@ -68,8 +68,6 @@
 
 	db := getDB()
 	for _, change := range changes.Changes {
-		log.Debugf("change table: %s operation: %s", change.Table, change.Operation)
-
 		var err error
 		switch change.Table {
 		case MANIFEST_TABLE:
diff --git a/listener_test.go b/listener_test.go
index a7cb03f..7160f35 100644
--- a/listener_test.go
+++ b/listener_test.go
@@ -85,40 +85,7 @@
 
 		deploymentID := "listener_test_2"
 
-		uri, err := url.Parse(testServer.URL)
-		Expect(err).ShouldNot(HaveOccurred())
-		uri.Path = "/bundle/1"
-		bundleUri := uri.String()
-
-		dep := deployment{
-			DeploymentID: deploymentID,
-			System: bundle{
-				URI: bundleUri,
-			},
-			Bundles: []bundle{
-				{
-					BundleID: "/bundle/1",
-					URI: bundleUri,
-					Scope: "some-scope",
-				},
-			},
-		}
-
-		depBytes, err := json.Marshal(dep)
-		Expect(err).ShouldNot(HaveOccurred())
-
-		row := common.Row{}
-		row["id"] = &common.ColumnVal{Value: deploymentID}
-		row["manifest_body"] = &common.ColumnVal{Value: string(depBytes)}
-
-		var event = common.ChangeList{}
-		event.Changes = []common.Change{
-			{
-				Operation: common.Insert,
-				Table: MANIFEST_TABLE,
-				NewRow: row,
-			},
-		}
+		var dep deployment
 
 		h := &test_handler{
 			deploymentID,
@@ -138,7 +105,9 @@
 		}
 
 		apid.Events().Listen(APIGEE_SYNC_EVENT, h)
-		apid.Events().Emit(APIGEE_SYNC_EVENT, &event)               // for standard listener
+
+		dep = triggerDeploymentEvent(deploymentID)
+
 		apid.Events().Emit(APIGEE_SYNC_EVENT, &common.ChangeList{}) // for test listener
 	})
 })
@@ -175,3 +144,45 @@
 		Expect(string(bytes)).Should(Equal(b.BundleID))
 	}
 }
+
+func triggerDeploymentEvent(deploymentID string) deployment {
+
+	uri, err := url.Parse(testServer.URL)
+	Expect(err).ShouldNot(HaveOccurred())
+	uri.Path = "/bundle/1"
+	bundleUri := uri.String()
+
+	dep := deployment{
+		DeploymentID: deploymentID,
+		System: bundle{
+			URI: bundleUri,
+		},
+		Bundles: []bundle{
+			{
+				BundleID: "/bundle/1",
+				URI: bundleUri,
+				Scope: "some-scope",
+			},
+		},
+	}
+
+	depBytes, err := json.Marshal(dep)
+	Expect(err).ShouldNot(HaveOccurred())
+
+	row := common.Row{}
+	row["id"] = &common.ColumnVal{Value: deploymentID}
+	row["manifest_body"] = &common.ColumnVal{Value: string(depBytes)}
+
+	var event = common.ChangeList{}
+	event.Changes = []common.Change{
+		{
+			Operation: common.Insert,
+			Table: MANIFEST_TABLE,
+			NewRow: row,
+		},
+	}
+
+	apid.Events().Emit(APIGEE_SYNC_EVENT, &event)
+
+	return dep
+}