mirror of https://github.com/databricks/cli.git
cancel test
This commit is contained in:
parent
3f48d1f41e
commit
549e49e2ae
|
@ -37,7 +37,7 @@ type UploadConfig struct {
|
||||||
// Upload reads telemetry logs from stdin and uploads them to the telemetry endpoint.
|
// Upload reads telemetry logs from stdin and uploads them to the telemetry endpoint.
|
||||||
// This function is always expected to be called in a separate child process from
|
// This function is always expected to be called in a separate child process from
|
||||||
// the main CLI process.
|
// the main CLI process.
|
||||||
func Upload() (*ResponseBody, error) {
|
func Upload(ctx context.Context) (*ResponseBody, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
b, err := io.ReadAll(os.Stdin)
|
b, err := io.ReadAll(os.Stdin)
|
||||||
|
@ -80,7 +80,7 @@ func Upload() (*ResponseBody, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set a maximum total time to try telemetry uploads.
|
// Set a maximum total time to try telemetry uploads.
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), maxUploadTime)
|
ctx, cancel := context.WithTimeout(ctx, maxUploadTime)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
resp := &ResponseBody{}
|
resp := &ResponseBody{}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package telemetry
|
package telemetry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -14,29 +15,7 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTelemetryUploadRetries(t *testing.T) {
|
func configureStdin(t *testing.T) {
|
||||||
server := testserver.New(t)
|
|
||||||
t.Cleanup(server.Close)
|
|
||||||
|
|
||||||
count := 0
|
|
||||||
server.Handle("POST", "/telemetry-ext", func(req testserver.Request) any {
|
|
||||||
count++
|
|
||||||
if count == 1 {
|
|
||||||
return ResponseBody{
|
|
||||||
NumProtoSuccess: 1,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if count == 2 {
|
|
||||||
return ResponseBody{
|
|
||||||
NumProtoSuccess: 2,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Setenv("DATABRICKS_HOST", server.URL)
|
|
||||||
t.Setenv("DATABRICKS_TOKEN", "token")
|
|
||||||
|
|
||||||
logs := []protos.FrontendLog{
|
logs := []protos.FrontendLog{
|
||||||
{
|
{
|
||||||
FrontendLogEventID: uuid.New().String(),
|
FrontendLogEventID: uuid.New().String(),
|
||||||
|
@ -76,9 +55,50 @@ func TestTelemetryUploadRetries(t *testing.T) {
|
||||||
f.Close()
|
f.Close()
|
||||||
os.Stdin = old
|
os.Stdin = old
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := Upload()
|
func TestTelemetryUploadRetries(t *testing.T) {
|
||||||
|
server := testserver.New(t)
|
||||||
|
t.Cleanup(server.Close)
|
||||||
|
|
||||||
|
count := 0
|
||||||
|
server.Handle("POST", "/telemetry-ext", func(req testserver.Request) any {
|
||||||
|
count++
|
||||||
|
if count == 1 {
|
||||||
|
return ResponseBody{
|
||||||
|
NumProtoSuccess: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if count == 2 {
|
||||||
|
return ResponseBody{
|
||||||
|
NumProtoSuccess: 2,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Setenv("DATABRICKS_HOST", server.URL)
|
||||||
|
t.Setenv("DATABRICKS_TOKEN", "token")
|
||||||
|
|
||||||
|
configureStdin(t)
|
||||||
|
|
||||||
|
resp, err := Upload(context.Background())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, int64(2), resp.NumProtoSuccess)
|
assert.Equal(t, int64(2), resp.NumProtoSuccess)
|
||||||
assert.Equal(t, 2, count)
|
assert.Equal(t, 2, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTelemetryUploadCanceled(t *testing.T) {
|
||||||
|
server := testserver.New(t)
|
||||||
|
t.Cleanup(server.Close)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
configureStdin(t)
|
||||||
|
_, err := Upload(ctx)
|
||||||
|
|
||||||
|
// Since the context is already cancelled, upload should fail immediately
|
||||||
|
// with a timeout error.
|
||||||
|
assert.ErrorContains(t, err, "Failed to flush telemetry log due to timeout")
|
||||||
|
}
|
||||||
|
|
2
main.go
2
main.go
|
@ -41,7 +41,7 @@ func main() {
|
||||||
Level: logger.LevelError,
|
Level: logger.LevelError,
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := telemetry.Upload()
|
resp, err := telemetry.Upload(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(errW, "error: %s\n", err)
|
fmt.Fprintf(errW, "error: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
Loading…
Reference in New Issue