github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/puller/sorter/memory_backend_test.go (about) 1 // Copyright 2021 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package sorter 15 16 import ( 17 "runtime" 18 "sync/atomic" 19 "time" 20 21 "github.com/pingcap/check" 22 "github.com/pingcap/ticdc/cdc/model" 23 "github.com/pingcap/ticdc/pkg/util/testleak" 24 ) 25 26 type memoryBackendSuite struct{} 27 28 var _ = check.SerialSuites(&memoryBackendSuite{}) 29 30 func (s *memoryBackendSuite) TestNoLeaking(c *check.C) { 31 defer testleak.AfterTest(c)() 32 33 bknd := newMemoryBackEnd() 34 wrtr, err := bknd.writer() 35 c.Assert(err, check.IsNil) 36 37 var objCount int64 38 for i := 0; i < 10000; i++ { 39 atomic.AddInt64(&objCount, 1) 40 event := model.NewResolvedPolymorphicEvent(0, 1) 41 runtime.SetFinalizer(event, func(*model.PolymorphicEvent) { 42 atomic.AddInt64(&objCount, -1) 43 }) 44 err := wrtr.writeNext(event) 45 c.Assert(err, check.IsNil) 46 } 47 err = wrtr.flushAndClose() 48 c.Assert(err, check.IsNil) 49 50 rdr, err := bknd.reader() 51 c.Assert(err, check.IsNil) 52 53 for i := 0; i < 5000; i++ { 54 _, err := rdr.readNext() 55 c.Assert(err, check.IsNil) 56 } 57 58 for i := 0; i < 10; i++ { 59 runtime.GC() 60 if atomic.LoadInt64(&objCount) <= 5000 { 61 break 62 } 63 time.Sleep(100 * time.Millisecond) 64 } 65 c.Assert(atomic.LoadInt64(&objCount), check.LessEqual, int64(5000)) 66 67 err = rdr.resetAndClose() 68 c.Assert(err, check.IsNil) 69 70 for i := 0; i < 10; i++ { 71 runtime.GC() 72 if atomic.LoadInt64(&objCount) == 0 { 73 break 74 } 75 time.Sleep(100 * time.Millisecond) 76 } 77 c.Assert(atomic.LoadInt64(&objCount), check.Equals, int64(0)) 78 }