MCPcopy Index your code
hub / github.com/upper/db / TestPreparedStatementsCache

Method TestPreparedStatementsCache

internal/testsuite/sql_suite.go:91–181  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

89}
90
91func (s *SQLTestSuite) TestPreparedStatementsCache() {
92 sess := s.Session()
93
94 sess.SetPreparedStatementCache(true)
95 defer sess.SetPreparedStatementCache(false)
96
97 var tMu sync.Mutex
98 tFatal := func(err error) {
99 tMu.Lock()
100 defer tMu.Unlock()
101
102 s.T().Errorf("tmu: %v", err)
103 }
104
105 // This limit was chosen because, by default, MySQL accepts 16k statements
106 // and dies. See https://github.com/upper/db/issues/287
107 limit := 20000
108
109 if detectrace.WithRace() {
110 // When running this test under the Go race detector we quickly reach the limit
111 // of 8128 alive goroutines it can handle, so we set it to a safer number.
112 //
113 // Note that in order to fully stress this feature you'll have to run this
114 // test without the race detector.
115 limit = 100
116 }
117
118 var wg sync.WaitGroup
119
120 for i := 0; i < limit; i++ {
121 wg.Add(1)
122 go func(i int) {
123 defer wg.Done()
124
125 // This query is different on each iteration and generates a new
126 // prepared statement everytime it's called.
127 res := sess.Collection("artist").Find().Select(db.Raw(fmt.Sprintf("count(%d) AS c", i)))
128
129 var count map[string]uint64
130 err := res.One(&count)
131 if err != nil {
132 tFatal(err)
133 }
134 }(i)
135 }
136 wg.Wait()
137
138 // Concurrent Insert can open many connections on MySQL / PostgreSQL, this
139 // sets a limit on them.
140 sess.SetMaxOpenConns(90)
141
142 switch s.Adapter() {
143 case "ql":
144 limit = 1000
145 }
146
147 for i := 0; i < limit; i++ {
148 wg.Add(1)

Callers

nothing calls this directly

Calls 14

RawMethod · 0.80
SessionMethod · 0.65
ErrorfMethod · 0.65
DoneMethod · 0.65
SelectMethod · 0.65
FindMethod · 0.65
CollectionMethod · 0.65
OneMethod · 0.65
WaitMethod · 0.65
SetMaxOpenConnsMethod · 0.65
AdapterMethod · 0.65

Tested by

no test coverage detected