|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2022 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 [email protected]. |
| 15 | + |
| 16 | +package testsuite |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 26 | + "github.com/arduino/go-paths-helper" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +func TestArduinoCliDaemon(t *testing.T) { |
| 31 | + env := NewEnvironment(t) |
| 32 | + defer env.CleanUp() |
| 33 | + |
| 34 | + cli := NewArduinoCliWithinEnvironment(t, paths.New("..", "..", "arduino-cli"), env) |
| 35 | + defer cli.CleanUp() |
| 36 | + |
| 37 | + _, _, err := cli.Run("core", "update-index") |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + _ = cli.StartDeamon(false) |
| 41 | + |
| 42 | + inst := cli.Create() |
| 43 | + require.NoError(t, inst.Init("", "", func(ir *commands.InitResponse) { |
| 44 | + fmt.Printf("INIT> %v\n", ir.GetMessage()) |
| 45 | + })) |
| 46 | + |
| 47 | + // Run a one-shot board list |
| 48 | + boardListResp, err := inst.BoardList(time.Second) |
| 49 | + require.NoError(t, err) |
| 50 | + fmt.Printf("Got boardlist response with %d ports\n", len(boardListResp.GetPorts())) |
| 51 | + |
| 52 | + // Run a one-shot board list again (should not fail) |
| 53 | + boardListResp, err = inst.BoardList(time.Second) |
| 54 | + require.NoError(t, err) |
| 55 | + fmt.Printf("Got boardlist response with %d ports\n", len(boardListResp.GetPorts())) |
| 56 | + |
| 57 | + testWatcher := func() { |
| 58 | + // Run watcher |
| 59 | + watcher, err := inst.BoardListWatch() |
| 60 | + require.NoError(t, err) |
| 61 | + ctx, cancel := context.WithCancel(context.Background()) |
| 62 | + go func() { |
| 63 | + defer cancel() |
| 64 | + for { |
| 65 | + msg, err := watcher.Recv() |
| 66 | + if err == io.EOF { |
| 67 | + fmt.Println("Watcher EOF") |
| 68 | + return |
| 69 | + } |
| 70 | + require.Empty(t, msg.Error, "Board list watcher returned an error") |
| 71 | + require.NoError(t, err, "BoardListWatch grpc call returned an error") |
| 72 | + fmt.Printf("WATCH> %v\n", msg) |
| 73 | + } |
| 74 | + }() |
| 75 | + time.Sleep(time.Second) |
| 76 | + require.NoError(t, watcher.CloseSend()) |
| 77 | + select { |
| 78 | + case <-ctx.Done(): |
| 79 | + // all right! |
| 80 | + case <-time.After(time.Second): |
| 81 | + require.Fail(t, "BoardListWatch didn't close") |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + testWatcher() |
| 86 | + testWatcher() |
| 87 | +} |
0 commit comments