1 Star 0 Fork 0

go-libs/process

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
export_test.go 2.27 KB
一键复制 编辑 原始数据 按行查看 历史
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <[email protected]>
// Date: 2024-12-17
package process
import (
"context"
"fmt"
"gitee.com/go-libs/runtime"
"testing"
"time"
)
type tp struct {
}
func (o *tp) Run(ctx context.Context) (err error) {
for {
select {
case <-ctx.Done():
return
}
}
}
type t0 struct {
name string
}
func (o *t0) After(_ context.Context) (err error) {
println(o.name, ".After")
return
}
func (o *t0) Before(_ context.Context) (err error) {
println(o.name, ".Before")
return
}
func (o *t0) Run(ctx context.Context) (err error) {
println(o.name, ".Run")
for {
select {
case <-ctx.Done():
return
}
}
}
func TestNew(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
proc := New("tester", &t0{name: "tester"})
go func() {
time.Sleep(time.Second)
cancel()
proc.Stop()
}()
t.Logf(`proc startup`)
if err := proc.Start(ctx); err != nil {
t.Logf(`start error: %v`, err)
return
}
t.Logf(`proc shutdown`)
}
func TestNewWithChild(t *testing.T) {
c := context.Background()
d := make(chan bool)
p := New("tester", &tp{})
go func() {
t.Logf(`startup`)
_ = p.Start(c)
t.Logf(`shutdown`)
}()
go func() {
time.Sleep(time.Second * 3)
d <- true
}()
var last Process
time.Sleep(time.Millisecond * 3)
for i := 0; i < 1000; i++ {
if last != nil {
last.Stop()
}
name := fmt.Sprintf(`test-%d`, i)
curr := New(name, &tp{})
p.Add(curr)
last = curr
// p.Del(fmt.Sprintf(`test-%d`, i-1))
// p.Add(New(fmt.Sprintf(`test-%d`, i), &tp{}))
}
for {
select {
case <-d:
p.StopWait()
x := runtime.GetCounter().ProcessCounter()
t.Logf(`counter: created=%d, started=%d, stopped=%d`, x.GetCreatedCount(), x.GetStartedCount(), x.GetStoppedCount())
return
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/go-libs/process.git
[email protected]:go-libs/process.git
go-libs
process
process
v2

搜索帮助