add test for timeout

This commit is contained in:
Shreyas Goenka 2025-02-24 15:26:31 +01:00
parent f11f5895ff
commit 0d37654735
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
9 changed files with 58 additions and 3 deletions

View File

@ -0,0 +1,12 @@
export DATABRICKS_CLI_TELEMETRY_PID_FILE=./telemetry.pid
export DATABRICKS_CLI_TELEMETRY_UPLOAD_LOGS_FILE=./out.upload_process.txt
trace $CLI selftest send-telemetry
echo "waiting for telemetry process to finish"
# Wait for the child telemetry process to finish
wait_pid $(cat ./telemetry.pid)
# cleanup the pid file
rm -f ./telemetry.pid

View File

@ -0,0 +1 @@
error: Failed to flush telemetry log due to timeout

View File

@ -0,0 +1,4 @@
>>> [CLI] selftest send-telemetry
waiting for telemetry process to finish
[wait_pid] process has ended

View File

@ -0,0 +1,16 @@
export DATABRICKS_CLI_TELEMETRY_PID_FILE=./telemetry.pid
export DATABRICKS_CLI_TELEMETRY_UPLOAD_LOGS_FILE=./out.upload_process.txt
# Configure a timeout of 0 seconds. This ensures that the timeout is respected and the
# telemetry process does not try to upload logs.
export DATABRICKS_CLI_TELEMETRY_UPLOAD_TIMEOUT="0s"
trace $CLI selftest send-telemetry
echo "waiting for telemetry process to finish"
# Wait for the child telemetry process to finish
wait_pid $(cat ./telemetry.pid)
# cleanup the pid file
rm -f ./telemetry.pid

View File

@ -0,0 +1,11 @@
[[Server]]
Pattern = "POST /telemetry-ext"
Response.Body = '''
{
"numProtoSuccess": 2
}
'''
[[Repls]]
Old = 'execution_time_ms\\\":\d{1,5},'
New = 'execution_time_ms\":\"SMALL_INT\",'

View File

@ -186,8 +186,7 @@ func workspaceClientOrPrompt(ctx context.Context, cfg *config.Config, allowPromp
return w, err return w, err
} }
// TODO: Make upload with oauth work. // TODO: Run as integration tests?
// TODO: Move env var inheritance to the daemon library.
func MustWorkspaceClient(cmd *cobra.Command, args []string) error { func MustWorkspaceClient(cmd *cobra.Command, args []string) error {
cfg := &config.Config{} cfg := &config.Config{}

View File

@ -103,6 +103,7 @@ func flagErrorFunc(c *cobra.Command, err error) error {
// Execute adds all child commands to the root command and sets flags appropriately. // Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd. // This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(ctx context.Context, cmd *cobra.Command) error { func Execute(ctx context.Context, cmd *cobra.Command) error {
// TODO: deferred panic recovery
ctx = telemetry.WithNewLogger(ctx) ctx = telemetry.WithNewLogger(ctx)
ctx = dbr.DetectRuntime(ctx) ctx = dbr.DetectRuntime(ctx)
start := time.Now() start := time.Now()

View File

@ -25,6 +25,9 @@ const (
// Environment variable to disable telemetry. If this is set to any value, telemetry // Environment variable to disable telemetry. If this is set to any value, telemetry
// will be disabled. // will be disabled.
DisableEnvVar = "DATABRICKS_CLI_DISABLE_TELEMETRY" DisableEnvVar = "DATABRICKS_CLI_DISABLE_TELEMETRY"
// Max time to try and upload the telemetry logs. Useful for testing.
UploadTimeout = "DATABRICKS_CLI_TELEMETRY_UPLOAD_TIMEOUT"
) )
type UploadConfig struct { type UploadConfig struct {
@ -68,8 +71,16 @@ func Upload() (*ResponseBody, error) {
return nil, fmt.Errorf("Failed to create API client: %s\n", err) return nil, fmt.Errorf("Failed to create API client: %s\n", err)
} }
maxUploadTime := 30 * time.Second
if v, ok := os.LookupEnv(UploadTimeout); ok {
maxUploadTime, err = time.ParseDuration(v)
if err != nil {
return nil, fmt.Errorf("Failed to parse time limit %s: %s\n", UploadTimeout, err)
}
}
// Set a maximum total time to try telemetry uploads. // Set a maximum total time to try telemetry uploads.
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) ctx, cancel := context.WithTimeout(context.Background(), maxUploadTime)
defer cancel() defer cancel()
resp := &ResponseBody{} resp := &ResponseBody{}