代码拉取完成,页面将自动刷新
// 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
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。