mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 16:04:24 +00:00
fix(grok): 补齐取消与调用方事务的回归断言
This commit is contained in:
1 parent
87266a7b18
commit
b1cb2eb1f3
2 files changed
+60
-13
No files matched your search
@@ -155,6 +155,45 @@ func TestGrokCatalogCASUsesTransactionClientAndSkipsCache(t *testing.T) {
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestGrokCatalogCASAppliesOnCallerTransactionNotBaseClient(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
nextJSON := jsonRaw(`{"models":["grok-4.7"]}`)
|
||||
baseDB, baseMock, err := sqlmock.New()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = baseDB.Close() })
|
||||
baseClient := dbent.NewClient(dbent.Driver(entsql.OpenDB(dialect.Postgres, baseDB)))
|
||||
t.Cleanup(func() { _ = baseClient.Close() })
|
||||
|
||||
txDB, txMock, err := sqlmock.New()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = txDB.Close() })
|
||||
txClient := dbent.NewClient(dbent.Driver(entsql.OpenDB(dialect.Postgres, txDB)))
|
||||
t.Cleanup(func() { _ = txClient.Close() })
|
||||
txMock.ExpectBegin()
|
||||
tx, err := txClient.Tx(context.Background())
|
||||
require.NoError(t, err)
|
||||
txMock.ExpectExec(regexp.QuoteMeta(grokCatalogCASQuery)).
|
||||
WithArgs(int64(25), "null", string(nextJSON)).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
exec := &recordingSQLExecutor{err: errors.New("must use transaction client")}
|
||||
cache := &grokCatalogCacheSpy{}
|
||||
repo := newAccountRepositoryWithSQL(baseClient, exec, cache)
|
||||
|
||||
applied, err := repo.CompareAndSwapGrokObservedModels(dbent.NewTxContext(context.Background(), tx), 25, nil, nextJSON)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.True(t, applied)
|
||||
require.Empty(t, exec.execQueries)
|
||||
require.Zero(t, cache.setAccounts)
|
||||
require.Zero(t, cache.bucketOps)
|
||||
require.NoError(t, baseMock.ExpectationsWereMet())
|
||||
txMock.ExpectRollback()
|
||||
require.NoError(t, tx.Rollback())
|
||||
require.NoError(t, txMock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestGrokCatalogCASRefreshesSingleAccountOutsideTransaction(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -239,6 +239,7 @@ func TestGrokCatalogPersistCancelStopsImmediately(t *testing.T) {
|
||||
t.Run("already canceled", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := newGrokCatalogMemoryRepo(grokCatalogPersistAccount())
|
||||
repo.writesWithoutContextCheck = true
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
@@ -248,11 +249,13 @@ func TestGrokCatalogPersistCancelStopsImmediately(t *testing.T) {
|
||||
require.ErrorIs(t, err, context.Canceled)
|
||||
require.Zero(t, repo.getCalls)
|
||||
require.Zero(t, repo.casCalls)
|
||||
require.Equal(t, []string{"kept"}, parseGrokObservedModels(repo.account.Extra).Models)
|
||||
})
|
||||
|
||||
t.Run("canceled between attempts", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := newGrokCatalogMemoryRepo(grokCatalogPersistAccount())
|
||||
repo.writesWithoutContextCheck = true
|
||||
repo.conflictsRemaining = 3
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
repo.afterCAS = func() { cancel() }
|
||||
@@ -445,15 +448,16 @@ func (r *grokCatalogMismatchedIDRepo) GetByID(ctx context.Context, id int64) (*A
|
||||
// grokCatalogMemoryRepo is the thread-safe account fixture shared with later catalog persistence tests.
|
||||
type grokCatalogMemoryRepo struct {
|
||||
AccountRepository
|
||||
mu sync.Mutex
|
||||
account *Account
|
||||
deleted bool
|
||||
getCalls int
|
||||
casCalls int
|
||||
updateExtraCalls int
|
||||
conflictsRemaining int
|
||||
onConflict func(*Account)
|
||||
afterCAS func()
|
||||
mu sync.Mutex
|
||||
account *Account
|
||||
deleted bool
|
||||
getCalls int
|
||||
casCalls int
|
||||
updateExtraCalls int
|
||||
conflictsRemaining int
|
||||
writesWithoutContextCheck bool
|
||||
onConflict func(*Account)
|
||||
afterCAS func()
|
||||
}
|
||||
|
||||
func newGrokCatalogMemoryRepo(account *Account) *grokCatalogMemoryRepo {
|
||||
@@ -461,8 +465,10 @@ func newGrokCatalogMemoryRepo(account *Account) *grokCatalogMemoryRepo {
|
||||
}
|
||||
|
||||
func (r *grokCatalogMemoryRepo) GetByID(ctx context.Context, id int64) (*Account, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
if !r.writesWithoutContextCheck {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
@@ -493,8 +499,10 @@ func (r *grokCatalogMemoryRepo) UpdateExtra(ctx context.Context, id int64, updat
|
||||
}
|
||||
|
||||
func (r *grokCatalogMemoryRepo) CompareAndSwapGrokObservedModels(ctx context.Context, id int64, expected, next json.RawMessage) (bool, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return false, err
|
||||
if !r.writesWithoutContextCheck {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
expectedJSON, err := normalizeGrokCatalogMemoryJSON(expected)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user