给 GPT 的 Prompts

avatar User

option.go // WithRetryCount sets the retry count for the pool. func WithRetryCount(retryCount int) Option { return func(p *goPool) { p.retryCount = retryCount } }
README.md

## Task Result Retrieval

GoPool supports task result retrieval. If a task returns a result, the result callback function will be called. This feature can be enabled by setting the WithResultCallback option when creating the pool.

Here is an example of how to use GoPool with task result retrieval:

package main

import (
    "fmt"

    "github.com/devchat-ai/gopool"
)

func main() {
    pool := gopool.NewGoPool(100, gopool.WithResultCallback(func(result interface{}) {
        fmt.Println("Task result:", result)
    }))
    defer pool.Release()

    for i := 0; i < 1000; i++ {
        pool.AddTask(func() (interface{}, error) {
            return "task result", nil
        })
    }
    pool.Wait()
}

In this example, if a task returns a result, the result will be printed to the console.

参考我给你的例子帮我在 README.md 中加一段英文的 Task Retry 的介绍

avatar DevChat

Based on the provided code and the existing README.md content, here is the English introduction for the Task Retry feature in GoPool:

Task Retry

GoPool supports task retry. If a task fails, it can be retried for a specified number of times. This feature can be enabled by setting the WithRetryCount option when creating the pool.

Here is an example of how to use GoPool with task retry:

package main

import (
    "errors"
    "fmt"

    "github.com/devchat-ai/gopool"
)

func main() {
    pool := gopool.NewGoPool(100, gopool.WithRetryCount(3))
    defer pool.Release()

    for i := 0; i < 1000; i++ {
        pool.AddTask(func() (interface{}, error) {
            return nil, errors.New("task error")
        })
    }
    pool.Wait()
}

In this example, if a task fails, it will be retried up to 3 times.

Please replace the existing content in README.md with the above content.

avatar User

然后请将这段介绍翻译成中文,我需要加到 README_zh.md 中

avatar DevChat

以下是对 GoPool 中的任务重试功能的中文介绍:

任务重试

GoPool 支持任务重试。如果任务失败,可以重试指定的次数。可以通过在创建池时设置 WithRetryCount 选项来启用此功能。

以下是如何使用带有任务重试的 GoPool 的示例:

package main

import (
    "errors"
    "fmt"

    "github.com/devchat-ai/gopool"
)

func main() {
    pool := gopool.NewGoPool(100, gopool.WithRetryCount(3))
    defer pool.Release()

    for i := 0; i < 1000; i++ {
        pool.AddTask(func() (interface{}, error) {
            return nil, errors.New("task error")
        })
    }
    pool.Wait()
}

在这个示例中,如果任务失败,它将重试最多3次。

请将上述内容添加到 README_zh.md 中。

avatar User

gopool_test.go func TestGoPoolWithResult(t *testing.T) { var expectedResult = "task result" pool := NewGoPool(100, WithResultCallback(func(result interface{}) { if result != expectedResult { t.Errorf("Expected result %v, but got %v", expectedResult, result) } })) defer pool.Release()
for i := 0; i < 1000; i++ {
    pool.AddTask(func() (interface{}, error) {
        return expectedResult, nil
    })
}
pool.Wait()

}

然后我希望你帮我加上 task retry 相关的 UT

avatar DevChat

Based on the existing unit test structure in gopool_test.go, here is a unit test for the task retry feature:

package gopool

import (
    "errors"
    "testing"
)

func TestGoPoolWithRetry(t *testing.T) {
    var retryCount = 3
    var taskError = errors.New("task error")
    var taskRunCount = 0

    pool := NewGoPool(100, WithRetryCount(retryCount))
    defer pool.Release()

    pool.AddTask(func() (interface{}, error) {
        taskRunCount++
        if taskRunCount <= retryCount {
            return nil, taskError
        }
        return nil, nil
    })

    pool.Wait()

    if taskRunCount != retryCount+1 {
        t.Errorf("Expected task to run %v times, but it ran %v times", retryCount+1, taskRunCount)
    }
}

This unit test adds a task to the pool that fails retryCount times before succeeding. After the pool has finished processing, it checks that the task has run retryCount + 1 times. If not, it fails the test.

Please add this unit test to your gopool_test.go file.