ROOTPLOIT
Server: LiteSpeed
System: Linux in-mum-web1878.main-hosting.eu 5.14.0-570.21.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 11 07:22:35 EDT 2025 x86_64
User: u435929562 (435929562)
PHP: 7.4.33
Disabled: system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //opt/go/pkg/mod/go.mongodb.org/[email protected]/mongo/background_context_test.go
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package mongo

import (
	"context"
	"testing"
	"time"

	"go.mongodb.org/mongo-driver/internal/assert"
)

func TestBackgroundContext(t *testing.T) {
	t.Run("newBackgroundContext accepts a nil child", func(t *testing.T) {
		ctx := newBackgroundContext(nil)
		assert.Equal(t, context.Background(), ctx, "expected context.Background() for a nil child")
	})
	t.Run("Value requests are forwarded", func(t *testing.T) {
		// Tests the Value function.

		type ctxKey struct{}
		expectedVal := "value"
		childCtx := context.WithValue(context.Background(), ctxKey{}, expectedVal)

		ctx := newBackgroundContext(childCtx)
		gotVal, ok := ctx.Value(ctxKey{}).(string)
		assert.True(t, ok, "expected context to contain a string value for ctxKey")
		assert.Equal(t, expectedVal, gotVal, "expected value for ctxKey to be %q, got %q", expectedVal, gotVal)
	})
	t.Run("background context does not have a deadline", func(t *testing.T) {
		// Tests the Deadline function.

		childCtx, cancel := context.WithTimeout(context.Background(), time.Second)
		defer cancel()

		ctx := newBackgroundContext(childCtx)
		deadline, ok := ctx.Deadline()
		assert.False(t, ok, "expected context to have no deadline, but got %v", deadline)
	})
	t.Run("background context cannot be cancelled", func(t *testing.T) {
		// Tests the Done and Err functions.

		childCtx, cancel := context.WithCancel(context.Background())
		cancel()

		ctx := newBackgroundContext(childCtx)
		select {
		case <-ctx.Done():
			t.Fatalf("expected context to not expire, but Done channel had a value; ctx error: %v", ctx.Err())
		default:
		}
		assert.Nil(t, ctx.Err(), "expected context error to be nil, got %v", ctx.Err())
	})
}