App引擎推送任务总是在测试中返回404

时间:2018-05-03 09:58:15

标签: google-app-engine go google-app-engine-go

我在Go App Engine应用程序中有一个推送任务队列。当我们试图将任务排入测试中时,无论出于何种原因,任务总是返回404.

我们的app.yaml:

runtime: go
api_version: go1.9

handlers:
 - url: /worker/.*
   script: _go_app
   login: admin
 - url: /.*
   script: _go_app

实际的任务调用:

func Handler(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)
    t := taskqueue.NewPOSTTask("/worker", map[string][]string{"key": {"val"}})
    _, err := taskqueue.Add(ctx, t, "")
    if err != nil {
        log.Errorf(ctx, "Failed to add task");
    }
    fmt.Fprintf(w, "Success");
}

仍然不完整的处理程序,但它存在!

func Worker(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)
    log.Infof(ctx, "Worker succeeded")
}

最后,证明我们实际上已经添加了路由器的路径:

func init() {
    http.HandleFunc("/", Handler)
    http.HandleFunc("/worker", Worker)
}

当我们实际运行测试时,我们总是得到以下日志记录输出:

INFO     2018-05-03 09:51:11,794 module.py:846] default: "POST /worker HTTP/1.1" 404 19
WARNING  2018-05-03 09:51:11,794 taskqueue_stub.py:2149] Task � failed to execute. This task will retry in 0.100 seconds
INFO     2018-05-03 09:51:11,897 module.py:846] default: "POST /worker HTTP/1.1" 404 19
WARNING  2018-05-03 09:51:11,897 taskqueue_stub.py:2149] Task � failed to execute. This task will retry in 0.200 seconds
INFO     2018-05-03 09:51:12,101 module.py:846] default: "POST /worker HTTP/1.1" 404 19
WARNING  2018-05-03 09:51:12,101 taskqueue_stub.py:2149] Task � failed to execute. This task will retry in 0.400 seconds

请注意,当我尝试通过像Paw这样的API客户端ping它时,/worker端点返回302,因此路径似乎已正确配置。只有当我尝试在测试中运行时才会出现404。

为什么这会回归404?我试过围绕他们documentation中的示例推送队列运行测试已遇到同样的问题 - 是否存在某种丢失的配置标志我无法传递给goapp

我推出了一个带有最小可复制示例的GitHub仓库here

2 个答案:

答案 0 :(得分:4)

运行goapp的顺序是自上而下,但您需要在app.yaml中具体说明。在你的情况下,这将有效:

package main

import (
    "fmt"
    "net/http"

    "google.golang.org/appengine"
    "google.golang.org/appengine/log"
    "google.golang.org/appengine/taskqueue"
)

func Handler(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)
    t := taskqueue.NewPOSTTask("/worker", map[string][]string{"key": {"val"}})
    _, err := taskqueue.Add(ctx, t, "")
    if err != nil {
        log.Errorf(ctx, "Failed to add task")
    }
    fmt.Fprintf(w, "Success")
}

func Worker(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)
    log.Infof(ctx, "Worker succeeded")
}

func init() {
    http.HandleFunc("/", Handler)
    http.HandleFunc("/worker", Worker)
}

为此你需要映射网址,如:

runtime: go
api_version: go1.9

handlers:
 - url: /worker
   script: _go_app
   login: admin
 - url: /.*
   script: _go_app

结果是:

enter image description here

看到工人正在跑两次。这是因为GET /favicon.ico正在进入GET /.*映射。所以这只是给你的细节!

更新(05/14/2018): 在您的测试中,您使用aetest.NewInstance(),它在ioutil.TempDir("", "appengine-aetest")中运行dev_appserver.py,编写您自己的main.go和app.yaml。请参阅上文instance_vm.go

i.appDir, err = ioutil.TempDir("", "appengine-aetest")
if err != nil {
    return err
}
defer func() {
    if err != nil {
        os.RemoveAll(i.appDir)
    }
}()
err = os.Mkdir(filepath.Join(i.appDir, "app"), 0755)
if err != nil {
    return err
}
err = ioutil.WriteFile(filepath.Join(i.appDir, "app", "app.yaml"), []byte(i.appYAML()), 0644)
  if err != nil {
    return err
}
err = ioutil.WriteFile(filepath.Join(i.appDir, "app", "stubapp.go"), []byte(appSource), 0644)
if err != nil {
    return err
}

//... others codes

const appYAMLTemplate = `
application: %s
version: 1
runtime: go
api_version: go1
vm: true
handlers:
- url: /.*
  script: _go_app
`

const appSource = `
package main
import "google.golang.org/appengine"
func main() { appengine.Main() }
`

所以你需要创建自己的服务器实例。这是一种方式:

//out buffer
var out bytes.Buffer
//start server
c := exec.Command("goapp", "serve") //default port=8080, adminPort=8000
c.Stdout = &out
c.Stderr = &out
c.Start()
defer c.Process.Kill()
//delay to wait server is completed
time.Sleep(10 * time.Second)

//... others codes

//quit server
quitReq, err := http.NewRequest("GET", "http://localhost:8000/quit", nil)
_, err := client.Do(quitReq)
if err != nil {
    fmt.Errorf("GET /quit handler error: %v", err)
}

要测试您的处理程序功能,请执行以下操作:

//create request (testing Handler func)
req, err := http.NewRequest("GET", "http://localhost:8080/", nil)
if err != nil {
    t.Fatal(err.Error())
}
//do GET
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
    t.Error(err)
}
defer resp.Body.Close()
//delay to wait for the worker to execute
time.Sleep(10 * time.Second)

要检索结果并测试它:

//read response
b, _ := ioutil.ReadAll(resp.Body)
resp_content := string(b)
//checking
if !strings.Contains(resp_content, "Handler Success") {
    t.Errorf("Handler not working")
}
//log server content
logserver := out.String()
if !strings.Contains(logserver, "Worker succeeded") {
    t.Errorf("Worker not working")
}
//log response
t.Logf(logserver)

结果是:

screenshot - test result

更新:Github的链接:https://github.com/ag-studies/go-appengine-sample

希望这有帮助!!

答案 1 :(得分:3)

您的handlers模式为/worker/.*,但您要向/worker发布任务。

您应该执行以下操作之一:

  1. 将模式更改为/worker.*或仅/worker
  2. 或向/worker//worker/some-task-name发出请求。