github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/metric/query_counter_test.go (about) 1 package metric_test 2 3 import ( 4 "errors" 5 6 "github.com/pf-qiu/concourse/v6/atc/db" 7 "github.com/pf-qiu/concourse/v6/atc/db/dbfakes" 8 "github.com/pf-qiu/concourse/v6/atc/metric" 9 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("Counting Database Queries", func() { 15 var ( 16 underlyingConn *dbfakes.FakeConn 17 countingConn db.Conn 18 ) 19 20 BeforeEach(func() { 21 underlyingConn = new(dbfakes.FakeConn) 22 countingConn = metric.CountQueries(underlyingConn) 23 }) 24 25 AfterEach(func() { 26 err := countingConn.Close() 27 Expect(err).NotTo(HaveOccurred()) 28 }) 29 30 It("passes through calls to the underlying connection", func() { 31 countingConn.Ping() 32 33 Expect(underlyingConn.PingCallCount()).To(Equal(1)) 34 }) 35 36 It("returns the return values from the underlying connection", func() { 37 underlyingConn.PingReturns(errors.New("disaster")) 38 39 err := countingConn.Ping() 40 Expect(err).To(MatchError("disaster")) 41 }) 42 43 Describe("query counting", func() { 44 It("increments the global (;_;) counter", func() { 45 _, err := countingConn.Query("SELECT $1::int", 1) 46 Expect(err).NotTo(HaveOccurred()) 47 48 Expect(metric.Metrics.DatabaseQueries.Delta()).To(Equal(float64(1))) 49 50 _, err = countingConn.Exec("SELECT $1::int", 1) 51 Expect(err).NotTo(HaveOccurred()) 52 53 countingConn.QueryRow("SELECT $1::int", 1) 54 55 Expect(metric.Metrics.DatabaseQueries.Delta()).To(Equal(float64(2))) 56 57 By("working in transactions") 58 underlyingTx := &dbfakes.FakeTx{} 59 underlyingConn.BeginReturns(underlyingTx, nil) 60 61 tx, err := countingConn.Begin() 62 Expect(err).NotTo(HaveOccurred()) 63 64 _, err = tx.Query("SELECT $1::int", 1) 65 Expect(err).NotTo(HaveOccurred()) 66 67 Expect(metric.Metrics.DatabaseQueries.Delta()).To(Equal(float64(1))) 68 }) 69 }) 70 })