@@ -21,6 +21,7 @@ import (
2121 "io"
2222 "testing"
2323
24+ "github.com/arduino/arduino-cli/internal/integrationtest"
2425 "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2526 "github.com/stretchr/testify/require"
2627)
@@ -41,57 +42,33 @@ func TestDaemonCoreUpdateIndex(t *testing.T) {
4142 ` "http://downloads.arduino.cc/package_inexistent_index.json"]` )
4243 require .NoError (t , err )
4344
45+ analyzeUpdateIndexClient := func (cl commands.ArduinoCoreService_UpdateIndexClient ) map [string ]* commands.DownloadProgressEnd {
46+ analyzer := integrationtest .NewDownloadProgressAnalyzer (t )
47+ for {
48+ msg , err := cl .Recv ()
49+ if err == io .EOF {
50+ break
51+ }
52+ require .NoError (t , err )
53+ analyzer .Process (msg .GetDownloadProgress ())
54+ }
55+ return analyzer .Results
56+ }
57+
4458 {
4559 cl , err := grpcInst .UpdateIndex (context .Background (), true )
4660 require .NoError (t , err )
47- res , err := analyzeUpdateIndexStream (t , cl )
48- require .NoError (t , err )
61+ res := analyzeUpdateIndexClient (cl )
4962 require .Len (t , res , 1 )
5063 require .True (t , res ["https://downloads.arduino.cc/packages/package_index.tar.bz2" ].Success )
5164 }
5265 {
5366 cl , err := grpcInst .UpdateIndex (context .Background (), false )
5467 require .NoError (t , err )
55- res , err := analyzeUpdateIndexStream (t , cl )
56- require .Error (t , err )
68+ res := analyzeUpdateIndexClient (cl )
5769 require .Len (t , res , 3 )
5870 require .True (t , res ["https://downloads.arduino.cc/packages/package_index.tar.bz2" ].Success )
5971 require .True (t , res ["http://arduino.esp8266.com/stable/package_esp8266com_index.json" ].Success )
6072 require .False (t , res ["http://downloads.arduino.cc/package_inexistent_index.json" ].Success )
6173 }
6274}
63-
64- // analyzeUpdateIndexStream runs an update index checking if the sequence of DownloadProgress and
65- // DownloadResult messages is correct. It returns a map reporting all the DownloadResults messages
66- // received (it maps urls to DownloadResults).
67- func analyzeUpdateIndexStream (t * testing.T , cl commands.ArduinoCoreService_UpdateIndexClient ) (map [string ]* commands.DownloadProgressEnd , error ) {
68- ongoingDownload := ""
69- results := map [string ]* commands.DownloadProgressEnd {}
70- for {
71- msg , err := cl .Recv ()
72- if err == io .EOF {
73- return results , nil
74- }
75- if err != nil {
76- return results , err
77- }
78- require .NoError (t , err )
79- fmt .Printf ("UPDATE> %+v\n " , msg )
80- if progress := msg .GetDownloadProgress (); progress != nil {
81- if start := progress .GetStart (); start != nil {
82- require .Empty (t , ongoingDownload , "DownloadProgressStart: started a download without 'completing' the previous one" )
83- ongoingDownload = start .Url
84- } else if update := progress .GetUpdate (); update != nil {
85- require .NotEmpty (t , ongoingDownload , "DownloadProgressUpdate: received update, but the download is not yet started..." )
86- } else if end := progress .GetEnd (); end != nil {
87- require .NotEmpty (t , ongoingDownload , "DownloadProgress: received a 'completed' notification but never initiated a download" )
88- results [ongoingDownload ] = end
89- ongoingDownload = ""
90- } else {
91- require .FailNow (t , "DownloadProgress: received an empty DownloadProgress (without Start, Update or End)" )
92- }
93- } else {
94- require .FailNow (t , "DownloadProgress: received an empty message (without a DownloadProgress)" )
95- }
96- }
97- }
0 commit comments