MCPcopy
hub / github.com/go-gorm/gorm / TestPreparedStmtLruFromTransaction

Function TestPreparedStmtLruFromTransaction

tests/prepared_stmt_test.go:94–151  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

92}
93
94func TestPreparedStmtLruFromTransaction(t *testing.T) {
95 db, _ := OpenTestConnection(&gorm.Config{PrepareStmt: true, PrepareStmtMaxSize: 10, PrepareStmtTTL: 20 * time.Second})
96
97 tx := db.Begin()
98 defer func() {
99 if r := recover(); r != nil {
100 tx.Rollback()
101 }
102 }()
103 if err := tx.Error; err != nil {
104 t.Errorf("Failed to start transaction, got error %v\n", err)
105 }
106
107 if err := tx.Where("name=?", "zzjin").Delete(&User{}).Error; err != nil {
108 tx.Rollback()
109 t.Errorf("Failed to run one transaction, got error %v\n", err)
110 }
111
112 if err := tx.Create(&User{Name: "zzjin"}).Error; err != nil {
113 tx.Rollback()
114 t.Errorf("Failed to run one transaction, got error %v\n", err)
115 }
116
117 if err := tx.Commit().Error; err != nil {
118 t.Errorf("Failed to commit transaction, got error %v\n", err)
119 }
120
121 if result := db.Where("name=?", "zzjin").Delete(&User{}); result.Error != nil || result.RowsAffected != 1 {
122 t.Fatalf("Failed, got error: %v, rows affected: %v", result.Error, result.RowsAffected)
123 }
124
125 tx2 := db.Begin()
126 if result := tx2.Where("name=?", "zzjin").Delete(&User{}); result.Error != nil || result.RowsAffected != 0 {
127 t.Fatalf("Failed, got error: %v, rows affected: %v", result.Error, result.RowsAffected)
128 }
129
130 tx2.Commit()
131 // Attempt to convert the connection pool of tx to the *gorm.PreparedStmtDB type.
132 // If the conversion is successful, ok will be true and conn will be the converted object;
133 // otherwise, ok will be false and conn will be nil.
134 conn, ok := tx.ConnPool.(*gorm.PreparedStmtDB)
135 // Get the number of statement keys stored in the PreparedStmtDB.
136 lens := len(conn.Stmts.Keys())
137 // Check if the number of stored statement keys is 0.
138 if lens == 0 {
139 // If the number is 0, it means there are no statements stored in the LRU cache.
140 // The test fails and an error message is output.
141 t.Fatalf("lru should not be empty")
142 }
143 // Wait for 40 seconds to give the statements in the cache enough time to expire.
144 time.Sleep(time.Second * 40)
145 // Assert whether the connection pool of tx is successfully converted to the *gorm.PreparedStmtDB type.
146 AssertEqual(t, ok, true)
147 // Assert whether the number of statement keys stored in the PreparedStmtDB is 0 after 40 seconds.
148 // If it is not 0, it means the statements in the cache have not expired as expected.
149 AssertEqual(t, len(conn.Stmts.Keys()), 0)
150
151}

Callers

nothing calls this directly

Calls 9

OpenTestConnectionFunction · 0.85
BeginMethod · 0.80
RollbackMethod · 0.65
DeleteMethod · 0.65
WhereMethod · 0.65
CreateMethod · 0.65
CommitMethod · 0.65
KeysMethod · 0.65
AssertEqualFunction · 0.50

Tested by

no test coverage detected