github.com/kaydxh/golang@v0.0.131/pkg/pool/instance/pool.instance_test.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package instance_test 23 24 import ( 25 "context" 26 "sync" 27 "testing" 28 "time" 29 30 instance_ "github.com/kaydxh/golang/pkg/pool/instance" 31 "github.com/sirupsen/logrus" 32 ) 33 34 type InstancePool struct { 35 pool *instance_.Pool 36 } 37 38 type Sdk struct { 39 } 40 41 func (s *Sdk) DoSdk() error { 42 logrus.Infof("do DoSdk") 43 time.Sleep(2 * time.Second) 44 return nil 45 } 46 47 func (s *InstancePool) InvokeProcess(ctx context.Context) error { 48 //logrus.Infof("do InvokeProcess") 49 resp, err := s.pool.Invoke(ctx, func(ctx context.Context, instance interface{}) (interface{}, error) { 50 return nil, instance.(*Sdk).DoSdk() 51 }) 52 if err != nil { 53 logrus.Errorf("do DoSdk err: %v", err) 54 } 55 _ = resp 56 57 return err 58 } 59 60 func GlobalInit() error { 61 logrus.Infof("do GlobaInit") 62 return nil 63 } 64 65 func GlobalRelease() error { 66 logrus.Infof("do GlobalRelease") 67 return nil 68 } 69 70 func LocalInit() error { 71 logrus.Infof("do LocalInit") 72 return nil 73 } 74 75 func LocalRelease() error { 76 logrus.Infof("do LocalRelease") 77 return nil 78 } 79 80 func New() error { 81 logrus.Infof("do New") 82 return nil 83 } 84 85 func Delete() { 86 logrus.Infof("do Delete") 87 } 88 89 func TestNewPool(t *testing.T) { 90 ctx := context.Background() 91 pool, err := instance_.NewPool(func() interface{} { 92 New() 93 return &Sdk{} 94 }, 95 instance_.WithCoreIDs([]int64{0, 1, 2}), 96 instance_.WithBatchSize(8), 97 instance_.WithWaitTimeoutOnce(50*time.Millisecond), 98 instance_.WithWaitTimeoutTotal(time.Second), 99 instance_.WithName("test-instance"), 100 instance_.WithEnabledPrintCostTime(true), 101 //instance_.WithWaitTimeout(time.Millisecond), 102 instance_.WithResevePoolSizePerCore(1), 103 instance_.WithCapacityPoolSizePerCore(1), 104 instance_.WithGlobalInitFunc(func() error { 105 return GlobalInit() 106 }), 107 instance_.WithGlobalReleaseFunc(func() error { 108 return GlobalRelease() 109 }), 110 instance_.WithLocalInitFunc(func(instace interface{}) error { 111 return LocalInit() 112 }), 113 instance_.WithLocalReleaseFunc(func(instace interface{}) error { 114 return LocalRelease() 115 }), 116 instance_.WithDeleteFunc(func(instace interface{}) { 117 Delete() 118 }), 119 ) 120 121 err = pool.GlobalInit(ctx) 122 if err != nil { 123 t.Errorf("global init err: %v", err) 124 return 125 } 126 127 defer pool.GlobalRelease(ctx) 128 129 //do somthing 130 instancePool := &InstancePool{ 131 pool: pool, 132 } 133 134 var wg sync.WaitGroup 135 for i := 0; i < 10; i++ { 136 wg.Add(1) 137 go func() { 138 defer wg.Done() 139 err = instancePool.InvokeProcess(ctx) 140 }() 141 } 142 143 wg.Wait() 144 145 return 146 147 }