@@ -49,3 +49,90 @@ func TestExecutor(t *testing.T) {
4949 t .Fatalf ("gotTotalCount %d != totalCount %d" , gotTotalCount , totalCount )
5050 }
5151}
52+
53+ // go test -v -cover -run=^TestExecutorMinMax$
54+ func TestExecutorMinMax (t * testing.T ) {
55+ executor := NewExecutor (minWorkers - 1 )
56+
57+ if executor .workers != minWorkers {
58+ t .Fatalf ("got %d != want %d" , executor .workers , minWorkers )
59+ }
60+
61+ executor .Close ()
62+ executor = NewExecutor (maxWorkers + 1 )
63+
64+ if executor .workers != maxWorkers {
65+ t .Fatalf ("got %d != want %d" , executor .workers , maxWorkers )
66+ }
67+
68+ executor .Close ()
69+ }
70+
71+ // go test -v -cover -run=^TestExecutorContext$
72+ func TestExecutorContext (t * testing.T ) {
73+ executor := NewExecutor (4 , WithQueueSize (1 ))
74+ defer executor .Close ()
75+
76+ got := uint (cap (executor .tasks ))
77+ if got != executor .conf .queueSize {
78+ t .Fatalf ("got %d != want %d" , got , executor .conf .queueSize )
79+ }
80+
81+ ctx , cancel := context .WithTimeout (context .Background (), 100 * time .Millisecond )
82+ defer cancel ()
83+
84+ for i := uint (0 ); i <= executor .workers ; i ++ {
85+ err := executor .Submit (ctx , func () {
86+ time .Sleep (200 * time .Millisecond )
87+ })
88+
89+ if err != nil {
90+ t .Fatal (err )
91+ }
92+ }
93+
94+ err := executor .Submit (ctx , func () {})
95+ if err != context .DeadlineExceeded {
96+ t .Fatalf ("got %+v != want %+v" , err , context .DeadlineExceeded )
97+ }
98+ }
99+
100+ // go test -v -cover -run=^TestExecutorClose$
101+ func TestExecutorClose (t * testing.T ) {
102+ executor := NewExecutor (4 , WithQueueSize (1 ))
103+ defer executor .Close ()
104+
105+ if executor .closed .Load () {
106+ t .Fatal ("executor is closed" )
107+ }
108+
109+ ctx := context .Background ()
110+ for i := uint (0 ); i <= executor .workers ; i ++ {
111+ err := executor .Submit (ctx , func () {
112+ time .Sleep (200 * time .Millisecond )
113+ })
114+
115+ if err != nil {
116+ t .Fatal (err )
117+ }
118+ }
119+
120+ go func () {
121+ err := executor .Submit (ctx , func () {})
122+ if err != ErrExecutorClosed {
123+ t .Errorf ("got %+v != want %+v" , err , ErrExecutorClosed )
124+ }
125+ }()
126+
127+ time .Sleep (10 * time .Millisecond )
128+ executor .Close ()
129+
130+ if ! executor .closed .Load () {
131+ t .Fatal ("executor not closed" )
132+ }
133+
134+ err := executor .Submit (ctx , func () {})
135+ if err != ErrExecutorClosed {
136+ t .Errorf ("got %+v != want %+v" , err , ErrExecutorClosed )
137+ }
138+ }
0 commit comments