Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

1.6 KB
Raw Blame
package main

import (
	"testing"

	gocql "github.com/apache/cassandra-gocql-driver/v2"
)

func TestInvalidateMetadataSessionDefersCloseUntilOperationsFinish(t *testing.T) {
	runtime := &connectionRuntime{
		sessions:        map[string]*gocql.Session{"": {}},
		permits:         make(chan struct{}, 2),
		metadataPermits: make(chan struct{}, 1),
	}

	releaseFirst, err := runtime.acquire(false)
	if err != nil {
		t.Fatal(err)
	}
	releaseSecond, err := runtime.acquire(false)
	if err != nil {
		t.Fatal(err)
	}
	metadataSession := runtime.sessions[""]

	runtime.invalidateMetadataSession()
	if metadataSession.Closed() {
		t.Fatal("metadata session closed while operations were active")
	}
	if len(runtime.retiredSessions) != 1 {
		t.Fatalf("unexpected retired session count: %d", len(runtime.retiredSessions))
	}

	releaseFirst()
	if metadataSession.Closed() {
		t.Fatal("metadata session closed before the final operation completed")
	}

	releaseSecond()
	if !metadataSession.Closed() {
		t.Fatal("metadata session was not closed after the final operation completed")
	}
	if len(runtime.retiredSessions) != 0 {
		t.Fatalf("retired sessions were not cleared: %d", len(runtime.retiredSessions))
	}
}

func TestInvalidateMetadataSessionClosesImmediatelyWithoutOperations(t *testing.T) {
	metadataSession := &gocql.Session{}
	runtime := &connectionRuntime{sessions: map[string]*gocql.Session{"": metadataSession}}

	runtime.invalidateMetadataSession()

	if !metadataSession.Closed() {
		t.Fatal("idle metadata session was not closed immediately")
	}
	if _, exists := runtime.sessions[""]; exists {
		t.Fatal("invalidated metadata session remains cached")
	}
}