[ISSUE-66918282] update tests, improve style
diff --git a/api.go b/api.go index 321cb96..5f5ab0e 100644 --- a/api.go +++ b/api.go
@@ -408,7 +408,7 @@ func getHttpHost() string { - configuredEndpoint := config.GetString(configBundleBlobDownloadEndpoint) + configuredEndpoint := config.GetString(configBlobDownloadEndpoint) if configuredEndpoint != "" { return configuredEndpoint }
diff --git a/apidGatewayConfDeploy_suite_test.go b/apidGatewayConfDeploy_suite_test.go index 0c859f7..a85014a 100644 --- a/apidGatewayConfDeploy_suite_test.go +++ b/apidGatewayConfDeploy_suite_test.go
@@ -50,7 +50,7 @@ config.Set(configApiServerBaseURI, "http://localhost") config.Set(configDebounceDuration, "1ms") config.Set(configDownloadQueueSize, 1) - config.Set(configBundleCleanupDelay, time.Millisecond) + config.Set(configBlobCleanupDelay, time.Millisecond) apid.InitializePlugins("0.0.0") go apid.API().Listen() time.Sleep(1 * time.Second)
diff --git a/bundle.go b/bundle.go index 5daf0a0..ab7e073 100644 --- a/bundle.go +++ b/bundle.go
@@ -150,10 +150,10 @@ } } b.blobs = ids - log.Debug("Attempt to download blobs, len: %v", len(b.blobs)) + log.Debugf("Attempt to download blobs, len: %v", len(b.blobs)) if len(b.blobs) == 0 && b.callback != nil { - b.callback() + go b.callback() return } @@ -181,7 +181,7 @@ attempted bool } -func (r *DownloadRequest) downloadBundle() error { +func (r *DownloadRequest) downloadBlob() error { log.Debugf("starting bundle download attempt for blobId=%s", r.blobId) var err error @@ -355,7 +355,7 @@ for req := range w.bm.downloadQueue { log.Debugf("starting download blobId=%s", req.blobId) - err := req.downloadBundle() + err := req.downloadBlob() if err != nil { // timeout if _, ok := err.(*timeoutError); ok {
diff --git a/bundle_test.go b/bundle_test.go index c4ffacd..fc1ec0d 100644 --- a/bundle_test.go +++ b/bundle_test.go
@@ -66,7 +66,7 @@ // init dummy api manager dummyApiMan = &dummyApiManager{ - notifyChan: make(chan int, 1), + notifyChan: make(chan bool, 1), initCalled: make(chan bool), } @@ -142,6 +142,28 @@ time.Sleep(time.Second) Expect(req.markFailedAt.IsZero()).Should(BeTrue()) }, 4) + + It("should call callback func after a round of download attempts", func() { + // download blobs + var ids []string + num := 1 + mathrand.Intn(5) + for i := 0; i < num; i++ { + ids = append(ids, util.GenerateUUID()) + } + finishChan := make(chan int) + testBundleMan.downloadBlobsWithCallback(ids, func() { + finishChan <- 1 + }) + for i := 0; i < num; i++ { + <-dummyDbMan.fileResponse + } + <-finishChan + // if there's no blob + testBundleMan.downloadBlobsWithCallback(nil, func() { + finishChan <- 1 + }) + <-finishChan + }, 1) }) Context("download blobs for changelist", func() { @@ -201,7 +223,7 @@ type dummyApiManager struct { initCalled chan bool - notifyChan chan int + notifyChan chan bool } func (a *dummyApiManager) InitAPI() { @@ -211,7 +233,7 @@ } func (a *dummyApiManager) notifyNewChange() { - a.notifyChan <- 1 + a.notifyChan <- true } type dummyBlobServer struct {
diff --git a/init.go b/init.go index 692d369..41ee499 100644 --- a/init.go +++ b/init.go
@@ -26,24 +26,24 @@ ) const ( - configProtocol = "protocol_type" - configAPIListen = "api_listen" - configBundleBlobDownloadEndpoint = "gatewaydeploy_bundle_download_endpoint" - configBundleDirKey = "gatewaydeploy_bundle_dir" - configDebounceDuration = "gatewaydeploy_debounce_duration" - configBundleCleanupDelay = "gatewaydeploy_bundle_cleanup_delay" - configMarkDeployFailedAfter = "gatewaydeploy_deployment_timeout" - configDownloadConnTimeout = "gatewaydeploy_download_connection_timeout" - configApiServerBaseURI = "apigeesync_proxy_server_base" - configApidInstanceID = "apigeesync_apid_instance_id" - configApidClusterID = "apigeesync_cluster_id" - configConcurrentDownloads = "apigeesync_concurrent_downloads" - configDownloadQueueSize = "apigeesync_download_queue_size" - configBlobServerBaseURI = "apigeesync_blob_server_base" - configStoragePath = "local_storage_path" - maxIdleConnsPerHost = 50 - httpTimeout = time.Minute - configBearerToken = "apigeesync_bearer_token" + configProtocol = "protocol_type" + configAPIListen = "api_listen" + configBlobDownloadEndpoint = "gatewaydeploy_bundle_download_endpoint" + configBlobDirKey = "gatewaydeploy_bundle_dir" + configDebounceDuration = "gatewaydeploy_debounce_duration" + configBlobCleanupDelay = "gatewaydeploy_bundle_cleanup_delay" + configMarkDeployFailedAfter = "gatewaydeploy_deployment_timeout" + configDownloadConnTimeout = "gatewaydeploy_download_connection_timeout" + configApiServerBaseURI = "apigeesync_proxy_server_base" + configApidInstanceID = "apigeesync_apid_instance_id" + configApidClusterID = "apigeesync_cluster_id" + configConcurrentDownloads = "apigeesync_concurrent_downloads" + configDownloadQueueSize = "apigeesync_download_queue_size" + configBlobServerBaseURI = "apigeesync_blob_server_base" + configStoragePath = "local_storage_path" + maxIdleConnsPerHost = 50 + httpTimeout = time.Minute + configBearerToken = "apigeesync_bearer_token" ) var ( @@ -81,9 +81,9 @@ return pluginData, fmt.Errorf("%s value %s parse err: %v", configApiServerBaseURI, apiServerBaseURI, err) } - config.SetDefault(configBundleDirKey, "bundles") + config.SetDefault(configBlobDirKey, "bundles") config.SetDefault(configDebounceDuration, time.Second) - config.SetDefault(configBundleCleanupDelay, time.Minute) + config.SetDefault(configBlobCleanupDelay, time.Minute) config.SetDefault(configMarkDeployFailedAfter, 5*time.Minute) config.SetDefault(configDownloadConnTimeout, 5*time.Minute) config.SetDefault(configConcurrentDownloads, 15) @@ -94,9 +94,9 @@ return pluginData, fmt.Errorf("%s must be a positive duration", configDebounceDuration) } - bundleCleanupDelay := config.GetDuration(configBundleCleanupDelay) + bundleCleanupDelay := config.GetDuration(configBlobCleanupDelay) if bundleCleanupDelay < time.Millisecond { - return pluginData, fmt.Errorf("%s must be a positive duration", configBundleCleanupDelay) + return pluginData, fmt.Errorf("%s must be a positive duration", configBlobCleanupDelay) } markDeploymentFailedAfter := config.GetDuration(configMarkDeployFailedAfter) @@ -145,7 +145,7 @@ // initialize bundle manager blobServerURL := config.GetString(configBlobServerBaseURI) - relativeBundlePath := config.GetString(configBundleDirKey) + relativeBundlePath := config.GetString(configBlobDirKey) storagePath := config.GetString(configStoragePath) bundlePath = path.Join(storagePath, relativeBundlePath) if err := os.MkdirAll(bundlePath, 0700); err != nil { @@ -169,9 +169,6 @@ bundleMan.initializeBundleDownloading() - //TODO initialize apiMan.distributeEvents() for api call with "block" - //go apiMan.distributeEvents() - // initialize event handler eventHandler = &apigeeSyncHandler{ dbMan: dbMan,
diff --git a/listener.go b/listener.go index 220997a..f77d306 100644 --- a/listener.go +++ b/listener.go
@@ -36,13 +36,6 @@ } } -type bundleConfigJson struct { - Name string `json:"name"` - URI string `json:"uri"` - ChecksumType string `json:"checksumType"` - Checksum string `json:"checksum"` -} - type apigeeSyncHandler struct { dbMan dbManagerInterface apiMan apiManagerInterface
diff --git a/listener_test.go b/listener_test.go index b9d3b2b..000a6e9 100644 --- a/listener_test.go +++ b/listener_test.go
@@ -37,7 +37,7 @@ eventHandler.stopListener(services) dummyApiMan = &dummyApiManager{ - notifyChan: make(chan int, 1), + notifyChan: make(chan bool, 1), initCalled: make(chan bool), } dummyDbMan = &dummyDbManager{ @@ -89,7 +89,7 @@ } }) - It("Snapshot events should set db version, and should only init API endpoint once", func() { + It("Snapshot events should set db version", func() { // emit snapshot for i := 0; i < 2+rand.Intn(5); i++ { @@ -100,9 +100,19 @@ <-apid.Events().Emit(APIGEE_SYNC_EVENT, snapshot) Expect(dummyDbMan.version).Should(Equal(version)) } + }) - // verify init API called - // Expect(<-dummyApiMan.initCalled).Should(BeTrue()) + It("Snapshot event should init API endpoint and notify long-polling", func() { + + // emit snapshot + version := fmt.Sprint(rand.Uint32()) + snapshot := &common.Snapshot{ + SnapshotInfo: version, + } + <-apid.Events().Emit(APIGEE_SYNC_EVENT, snapshot) + Expect(dummyDbMan.version).Should(Equal(version)) + Expect(<-dummyApiMan.initCalled).Should(BeTrue()) + Expect(<-dummyApiMan.notifyChan).Should(BeTrue()) }) }) @@ -297,6 +307,7 @@ bm.blobChan <- id } }() + go callback() } func (bm *dummyBundleManager) makeDownloadRequest(blobId string, bunchRequest *BunchDownloadRequest) *DownloadRequest {