From 71605f6447bb73106daca287e3939dde68235d7c Mon Sep 17 00:00:00 2001 From: andy-stark-redis <164213578+andy-stark-redis@users.noreply.github.com> Date: Wed, 9 Oct 2024 11:44:29 +0100 Subject: [PATCH] DOC-4328 added DEL, EXPIRE, and TTL command examples (#3143) Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com> --- .github/workflows/build.yml | 92 +++++++++++++++- commands_test.go | 4 +- doctests/cmds_generic_test.go | 194 ++++++++++++++++++++++++++++++++++ 3 files changed, 285 insertions(+), 5 deletions(-) create mode 100644 doctests/cmds_generic_test.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5007423a4f..e1f3fa537b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,9 +10,10 @@ permissions: contents: read jobs: - build: - name: build + test-makefile: + name: Build with Makefile runs-on: ubuntu-latest + strategy: fail-fast: false matrix: @@ -35,11 +36,94 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - name: Test + - name: Run tests using Makefile run: make test - name: Upload to Codecov uses: codecov/codecov-action@v4 with: files: coverage.txt - token: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file + token: ${{ secrets.CODECOV_TOKEN }} + + test-redis-8: + name: Build with Enhanced Tests + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + go-version: [1.19.x, 1.20.x, 1.21.x] + + services: + redis: + image: redis:8.0-M01 + options: >- + --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5 + ports: + - 6379:6379 + - 6380:6380 + - 6381:6381 + - 6390:6390 + - 6391:6391 + - 6392:6392 + - 9123:9123 + - 9124:9124 + - 9125:9125 + - 9126:9126 + - 9127:9127 + - 9128:9128 + - 8220:8220 + - 8221:8221 + - 8222:8222 + - 8223:8223 + - 8224:8224 + - 8225:8225 + + steps: + - name: Set up ${{ matrix.go-version }} + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Run enhanced tests + run: | + GO_MOD_DIRS=$(find . -type f -name 'go.mod' -exec dirname {} \; | sort) + GO_VERSION=$(go version | cut -d " " -f 3 | cut -d. -f2) + set -e + for dir in $GO_MOD_DIRS; do + if echo "$dir" | grep -q "./example" && [ "$GO_VERSION" = "19" ]; then + echo "Skipping go test in $dir due to Go version 1.19 and dir contains ./example" + continue + fi + echo "Running tests in $dir" + cd "$dir" + go mod tidy -compat=1.18 + go test --ginkgo.skip-file="gears_commands_test.go" + go test ./... -short -race --ginkgo.skip-file="gears_commands_test.go" + go test ./... -run=NONE -bench=. -benchmem --ginkgo.skip-file="gears_commands_test.go" + env GOOS=linux GOARCH=386 go test --ginkgo.skip-file="gears_commands_test.go" + go test -coverprofile=coverage.txt -covermode=atomic ./... --ginkgo.skip-file="gears_commands_test.go" + go vet + cd - + done + + - name: Build and run custom vet tool + run: | + cd internal/customvet + go build . + cd ../.. + go vet -vettool ./internal/customvet/customvet + + - name: Check code formatting + run: | + gofumpt -w ./ + goimports -w -local github.com/redis/go-redis ./ + + - name: Upload to Codecov + uses: codecov/codecov-action@v4 + with: + files: coverage.txt + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/commands_test.go b/commands_test.go index 9554bf9a9f..11295d3dc1 100644 --- a/commands_test.go +++ b/commands_test.go @@ -679,7 +679,9 @@ var _ = Describe("Commands", func() { Expect(set.Val()).To(Equal("OK")) migrate = client.Migrate(ctx, "localhost", redisSecondaryPort, "key", 0, 0) - Expect(migrate.Err()).To(MatchError("IOERR error or timeout writing to target instance")) + Expect(migrate.Err()).To(SatisfyAny( + MatchError("IOERR error or timeout writing to target instance"), + MatchError("IOERR error or timeout reading to target instance"))) Expect(migrate.Val()).To(Equal("")) }) diff --git a/doctests/cmds_generic_test.go b/doctests/cmds_generic_test.go new file mode 100644 index 0000000000..ab8ebdd53f --- /dev/null +++ b/doctests/cmds_generic_test.go @@ -0,0 +1,194 @@ +// EXAMPLE: cmds_generic +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + "math" + "time" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_del_cmd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "key1", "key2", "key3") + // REMOVE_END + + // STEP_START del + delResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(delResult1) // >>> OK + + delResult2, err := rdb.Set(ctx, "key2", "World", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(delResult2) // >>> OK + + delResult3, err := rdb.Del(ctx, "key1", "key2", "key3").Result() + + if err != nil { + panic(err) + } + + fmt.Println(delResult3) // >>> 2 + // STEP_END + + // Output: + // OK + // OK + // 2 +} + +func ExampleClient_expire_cmd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "mykey") + // REMOVE_END + + // STEP_START expire + expireResult1, err := rdb.Set(ctx, "mykey", "Hello", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult1) // >>> OK + + expireResult2, err := rdb.Expire(ctx, "mykey", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult2) // >>> true + + expireResult3, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(math.Round(expireResult3.Seconds())) // >>> 10 + + expireResult4, err := rdb.Set(ctx, "mykey", "Hello World", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult4) // >>> OK + + expireResult5, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult5) // >>> -1ns + + expireResult6, err := rdb.ExpireXX(ctx, "mykey", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult6) // >>> false + + expireResult7, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult7) // >>> -1ns + + expireResult8, err := rdb.ExpireNX(ctx, "mykey", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult8) // >>> true + + expireResult9, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(math.Round(expireResult9.Seconds())) // >>> 10 + // STEP_END + + // Output: + // OK + // true + // 10 + // OK + // -1ns + // false + // -1ns + // true + // 10 +} + +func ExampleClient_ttl_cmd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "mykey") + // REMOVE_END + + // STEP_START ttl + ttlResult1, err := rdb.Set(ctx, "mykey", "Hello", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(ttlResult1) // >>> OK + + ttlResult2, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(math.Round(ttlResult2.Seconds())) // >>> 10 + // STEP_END + + // Output: + // OK + // 10 +}