diff --git a/internal/cluster/clutser_test.go b/internal/cluster/clutser_test.go index e4fc808..2e378df 100644 --- a/internal/cluster/clutser_test.go +++ b/internal/cluster/clutser_test.go @@ -35,24 +35,34 @@ func createSimulationCluster(nums int) ([]*Cluster, error) { return result, nil } -func TestSingleClusterLifetime(t *testing.T) { - clusters, err := createSimulationCluster(1) - if err != nil { - t.Errorf("create simulation cluster failed: %v", err) - return +func launchSimulationCluster(clusters []*Cluster, t *testing.T) { + for _, cluster := range clusters { + cluster.Launch() } - clusters[0].Launch() - defer func() { - clusters[0].Close() +} + +func closeSimulationCluster(clusters []*Cluster, t *testing.T) { + for _, cluster := range clusters { + cluster.Close() // wait for the cluster to close time.Sleep(time.Second * 1) // check if the cluster is closed - _, err := cache.GetMapField[node](CLUSTER_STATUS_HASH_MAP_KEY, clusters[0].id) + _, err := cache.GetMapField[node](CLUSTER_STATUS_HASH_MAP_KEY, cluster.id) if err == nil { t.Errorf("cluster is not closed") return } - }() + } +} + +func TestSingleClusterLifetime(t *testing.T) { + clusters, err := createSimulationCluster(1) + if err != nil { + t.Errorf("create simulation cluster failed: %v", err) + return + } + launchSimulationCluster(clusters, t) + defer closeSimulationCluster(clusters, t) time.Sleep(time.Second * 1) @@ -69,24 +79,10 @@ func TestMultipleClusterLifetime(t *testing.T) { t.Errorf("create simulation cluster failed: %v", err) return } - - for _, cluster := range clusters { - cluster.Launch() - defer func(cluster *Cluster) { - cluster.Close() - // wait for the cluster to close - time.Sleep(time.Second * 1) - // check if the cluster is closed - _, err := cache.GetMapField[node](CLUSTER_STATUS_HASH_MAP_KEY, cluster.id) - if err == nil { - t.Errorf("cluster is not closed") - return - } - }(cluster) - } + launchSimulationCluster(clusters, t) + defer closeSimulationCluster(clusters, t) time.Sleep(time.Second * 1) - has_master := false for _, cluster := range clusters { @@ -117,21 +113,8 @@ func TestClusterSubstituteMaster(t *testing.T) { t.Errorf("create simulation cluster failed: %v", err) return } - - for _, cluster := range clusters { - cluster.Launch() - defer func(cluster *Cluster) { - cluster.Close() - // wait for the cluster to close - time.Sleep(time.Second * 1) - // check if the cluster is closed - _, err := cache.GetMapField[node](CLUSTER_STATUS_HASH_MAP_KEY, cluster.id) - if err == nil { - t.Errorf("cluster is not closed") - return - } - }(cluster) - } + launchSimulationCluster(clusters, t) + defer closeSimulationCluster(clusters, t) time.Sleep(time.Second * 1) @@ -184,21 +167,8 @@ func TestClusterAutoGCNoLongerActiveNode(t *testing.T) { t.Errorf("create simulation cluster failed: %v", err) return } - - for _, cluster := range clusters { - cluster.Launch() - defer func(cluster *Cluster) { - cluster.Close() - // wait for the cluster to close - time.Sleep(time.Second * 1) - // check if the cluster is closed - _, err := cache.GetMapField[node](CLUSTER_STATUS_HASH_MAP_KEY, cluster.id) - if err == nil { - t.Errorf("cluster is not closed") - return - } - }(cluster) - } + launchSimulationCluster(clusters, t) + defer closeSimulationCluster(clusters, t) time.Sleep(time.Second * 1) diff --git a/internal/cluster/plugin_test.go b/internal/cluster/plugin_test.go index d8d322c..1ec78e7 100644 --- a/internal/cluster/plugin_test.go +++ b/internal/cluster/plugin_test.go @@ -20,12 +20,3 @@ func getRandomPluginRuntime() entities.PluginRuntime { }, } } - -// func TestPluginScheduleLifetime(t *testing.T) { -// plugin := getRandomPluginRuntime() -// cluster, err := createSimulationCluster(1) -// if err != nil { -// t.Errorf("create simulation cluster failed: %v", err) -// return -// } -// }