2016-05-13 06:48:53 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
2016-02-07 00:43:56 +03:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package schedule
|
|
|
|
|
|
|
|
import (
|
2017-09-07 00:57:25 +03:00
|
|
|
"context"
|
2022-06-13 16:39:42 +03:00
|
|
|
"fmt"
|
2016-02-07 00:43:56 +03:00
|
|
|
"testing"
|
2022-06-13 16:39:42 +03:00
|
|
|
|
|
|
|
"go.uber.org/zap/zaptest"
|
2016-02-07 00:43:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFIFOSchedule(t *testing.T) {
|
2022-06-13 16:39:42 +03:00
|
|
|
s := NewFIFOScheduler(zaptest.NewLogger(t))
|
2016-02-07 00:43:56 +03:00
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
next := 0
|
|
|
|
jobCreator := func(i int) Job {
|
2022-06-13 16:39:42 +03:00
|
|
|
return NewJob(fmt.Sprintf("i_%d_increse", i), func(ctx context.Context) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
fmt.Println("err: ", err)
|
|
|
|
}
|
|
|
|
}()
|
2016-02-07 00:43:56 +03:00
|
|
|
if next != i {
|
2016-03-30 22:37:08 +03:00
|
|
|
t.Fatalf("job#%d: got %d, want %d", i, next, i)
|
2016-02-07 00:43:56 +03:00
|
|
|
}
|
|
|
|
next = i + 1
|
2022-06-13 16:39:42 +03:00
|
|
|
if next%3 == 0 {
|
|
|
|
panic("fifo panic")
|
|
|
|
}
|
|
|
|
})
|
2016-02-07 00:43:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var jobs []Job
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
jobs = append(jobs, jobCreator(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, j := range jobs {
|
|
|
|
s.Schedule(j)
|
|
|
|
}
|
|
|
|
|
2016-02-12 02:20:19 +03:00
|
|
|
s.WaitFinish(100)
|
2023-03-08 07:18:05 +03:00
|
|
|
if s.Finished() != 100 {
|
|
|
|
t.Errorf("finished = %d, want %d", s.Finished(), 100)
|
2016-02-07 00:43:56 +03:00
|
|
|
}
|
|
|
|
}
|