|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2021 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to license@arduino.cc. |
| 15 | + |
| 16 | +package executils |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestProcessWithinContext(t *testing.T) { |
| 27 | + // Build `delay` helper inside testdata/delay |
| 28 | + builder, err := NewProcess("go", "build") |
| 29 | + require.NoError(t, err) |
| 30 | + builder.SetDir("testdata/delay") |
| 31 | + require.NoError(t, builder.Run()) |
| 32 | + |
| 33 | + // Run delay and test if the process is terminated correctly due to context |
| 34 | + process, err := NewProcess("testdata/delay/delay") |
| 35 | + require.NoError(t, err) |
| 36 | + start := time.Now() |
| 37 | + ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond) |
| 38 | + err = process.RunWithinContext(ctx) |
| 39 | + require.Error(t, err) |
| 40 | + require.Less(t, time.Since(start), 500*time.Millisecond) |
| 41 | + cancel() |
| 42 | +} |
0 commit comments