Добавил тест

master
Rinsvent 2 months ago
parent 3a02c2003c
commit b695b2c73f
  1. 4
      go.mod
  2. 9
      go.sum
  3. 80
      thread_test.go

@ -6,10 +6,14 @@ require git.rinsvent.ru/rinsvent/gol v0.0.0-20240921094550-1489c64a01e7
require (
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/tchap/zapext v1.0.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.27.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

@ -2,15 +2,24 @@ git.rinsvent.ru/rinsvent/gol v0.0.0-20240921094550-1489c64a01e7 h1:RASDTYghzGaJv
git.rinsvent.ru/rinsvent/gol v0.0.0-20240921094550-1489c64a01e7/go.mod h1:U9bes/stAbl7rZ+3rZRCq2GcmzZIUAk6xgE4l8zis9o=
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d h1:S2NE3iHSwP0XV47EEXL8mWmRdEfGscSJ+7EgePNgt0s=
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tchap/zapext v1.0.0 h1:qPxfRLzqYzemT+Pgs5VoH8NGU5YS7cgCnhcqRGkmrXc=
github.com/tchap/zapext v1.0.0/go.mod h1:0VgDSQ0xHJRqkxrwu3G2i2762jSnAJMz7rYxiZGpW1U=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

@ -0,0 +1,80 @@
package thread
import (
"context"
"github.com/stretchr/testify/assert"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
)
type report struct {
rw sync.Mutex
text string
}
func (r *report) log(text string) {
r.rw.Lock()
defer r.rw.Unlock()
r.text += text
}
func (r *report) String() string {
return r.text
}
func Test_checkManager(t *testing.T) {
var count atomic.Int32
var r report
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
wg.Add(1)
go func() {
defer wg.Done()
r.log("Start manger\n")
manager := Manager{Count: 3}
go manager.Start(func() {
number := count.Add(1)
r.log("Start " + strconv.Itoa(int(number)) + "\n")
switch number {
case 1:
<-ctx.Done()
time.Sleep(100 * time.Millisecond)
case 2:
time.Sleep(100 * time.Millisecond)
case 3:
time.Sleep(200 * time.Millisecond)
case 4:
time.Sleep(200 * time.Millisecond)
case 5:
<-ctx.Done()
time.Sleep(200 * time.Millisecond)
case 6:
<-ctx.Done()
time.Sleep(300 * time.Millisecond)
default:
<-ctx.Done()
time.Sleep(400 * time.Millisecond)
}
r.log("Finish " + strconv.Itoa(int(number)) + "\n")
})
r.log("Wait cancel\n")
manager.Wait(ctx)
r.log("Finish manger\n")
}()
time.Sleep(1 * time.Second)
cancel()
wg.Wait()
assert.Equal(t, int32(6), count.Load())
assert.Equal(t, "Start manger\nWait cancel\nStart 1\nStart 2\nStart 3\nFinish 2\nStart 4\nFinish 3\nStart 5\nFinish 4\nStart 6\nFinish 1\nFinish 5\nFinish 6\nFinish manger\n", r.String())
}
Loading…
Cancel
Save