Shinichiro Hamaji | b69bf8a | 2015-06-10 14:52:06 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "path/filepath" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func TestPara(t *testing.T) { |
| 24 | cwd, err := filepath.Abs(".") |
| 25 | if err != nil { |
| 26 | panic(err) |
| 27 | } |
| 28 | katiDir = cwd |
Shinichiro Hamaji | a680842 | 2015-05-13 18:00:50 +0900 | [diff] [blame] | 29 | jobsFlag = 4 |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 30 | |
| 31 | paraChan := make(chan *ParaResult) |
| 32 | para := NewParaWorker(paraChan) |
| 33 | go para.Run() |
| 34 | |
Fumitoshi Ukai | 936de10 | 2015-06-08 11:21:16 +0900 | [diff] [blame] | 35 | numTasks := 100 |
| 36 | for i := 0; i < numTasks; i++ { |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 37 | runners := []runner{ |
| 38 | { |
| 39 | output: fmt.Sprintf("%d", i), |
| 40 | cmd: fmt.Sprintf("echo test%d 2>&1", i), |
| 41 | shell: "/bin/sh", |
| 42 | }, |
| 43 | } |
| 44 | para.RunCommand(runners) |
| 45 | } |
| 46 | |
Shinichiro Hamaji | a680842 | 2015-05-13 18:00:50 +0900 | [diff] [blame] | 47 | var started []*ParaResult |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 48 | var results []*ParaResult |
Fumitoshi Ukai | 936de10 | 2015-06-08 11:21:16 +0900 | [diff] [blame] | 49 | for len(started) != numTasks || len(results) != numTasks { |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 50 | select { |
| 51 | case r := <-paraChan: |
Shinichiro Hamaji | a680842 | 2015-05-13 18:00:50 +0900 | [diff] [blame] | 52 | fmt.Printf("started=%d finished=%d\n", len(started), len(results)) |
| 53 | if r.status < 0 && r.signal < 0 { |
| 54 | started = append(started, r) |
| 55 | } else { |
| 56 | results = append(results, r) |
| 57 | } |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | para.Wait() |
| 62 | } |