首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模拟context.Context测试lambdacontext.FromContext

模拟context.Context测试lambdacontext.FromContext
EN

Stack Overflow用户
提问于 2018-05-23 14:18:25
回答 1查看 5K关注 0票数 5

我正在使用aws-sdk-goaws-lambda-go构建一个aws,我遇到了一个小问题。

我想测试我的lambda处理程序。为此,我需要模拟一个有效的context.Context,其中包含lamdacontext.LambdaContext的有效属性并满足lambdacontext.FromContext

我似乎找不到构建这种模拟的方法,因为lambdacontext.FromContext总是返回给我_, false

下面是我的主语,其中有一个关于events.SNSEvent事件的简单处理程序:

代码语言:javascript
复制
package main

import (
    "context"
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/aws/aws-lambda-go/lambdacontext"
)

func main() {
    lambda.Start(handleRequest)
}

func handleRequest(ctx context.Context, snsEvent events.SNSEvent) error {
    lc, ok := lambdacontext.FromContext(ctx); if !ok {
        // Always false
        ...
        return someErr
    }
    . . .
    return nil
}

这是我的handleRequest测试

代码语言:javascript
复制
package main

import (
    "context"
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambdacontext"
    "github.com/stretchr/testify/assert"
    "gitlab.easy-network.it/meg/aml-rekognition/testdata"
    "testing"
)

const imgMock = `
{
  \"some_parameter\": \"some_value\"
}`

func TestHandleRequest(t *testing.T) {

    c := context.Background()

    ctxV := context.WithValue(c, "", map[string]interface{}{
        "AwsRequestID" : "some_aws_id",
        "InvokedFunctionArn" : "some_arn",
        "Identity" : lambdacontext.CognitoIdentity{},
        "ClientContext" : lambdacontext.ClientContext{},
    })

    snsEventMock := events.SNSEvent{
        Records: []events.SNSEventRecord{
            {
                SNS: events.SNSEntity{
                    Message: imgMock,
                },
            },
        },
    }

    err := handleRequest(ctxV, snsEventMock)
    assert.NoError(t, err)

}

我也尝试过其他的模拟,比如传递带有这些参数的结构等等,但是我总是得到false。例如,我也尝试过:

代码语言:javascript
复制
type TestMock struct {
    AwsRequestID string
    InvokedFunctionArn string
    Identity lambdacontext.CognitoIdentity
    ClientContext lambdacontext.ClientContext
}

func TestHandleRequest(t *testing.T) {

    c := context.Background()

    testMock := TestMock{
        AwsRequestID : "some_aws_id",
        InvokedFunctionArn : "some_arn",
        Identity : lambdacontext.CognitoIdentity{},
        ClientContext : lambdacontext.ClientContext{},
    }

    ctxV := context.WithValue(c, "", testMock)

    . . .

}

我查了一下FromContext的来源,我已经抓了一段时间了。

代码语言:javascript
复制
// LambdaContext is the set of metadata that is passed for every Invoke.
type LambdaContext struct {
    AwsRequestID       string
    InvokedFunctionArn string
    Identity           CognitoIdentity
    ClientContext      ClientContext
}

// An unexported type to be used as the key for types in this package.
// This prevents collisions with keys defined in other packages.
type key struct{}

// The key for a LambdaContext in Contexts.
// Users of this package must use lambdacontext.NewContext and 
lambdacontext.FromContext

// instead of using this key directly.
var contextKey = &key{}

// FromContext returns the LambdaContext value stored in ctx, if any.
func FromContext(ctx context.Context) (*LambdaContext, bool) {
    lc, ok := ctx.Value(contextKey).(*LambdaContext)
    return lc, ok
}

当然,即使我只是将一个false传递给它,它也会返回context.Background()

对于如何构建一个有效的context.Contextlambdacontext.FromContext返回true有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-23 14:29:07

lambda.FromContext()检查传递的context.Context是否包含一个在lambdacontext包中持有“私有”键的值:

代码语言:javascript
复制
// An unexported type to be used as the key for types in this package.
// This prevents collisions with keys defined in other packages.
type key struct{}

// The key for a LambdaContext in Contexts.
// Users of this package must use lambdacontext.NewContext and lambdacontext.FromContext
// instead of using this key directly.
var contextKey = &key{}

// FromContext returns the LambdaContext value stored in ctx, if any.
func FromContext(ctx context.Context) (*LambdaContext, bool) {
    lc, ok := ctx.Value(contextKey).(*LambdaContext)
    return lc, ok
}

您无法访问此键,也无法“复制”它(您无法创建与此“私有”键相等的值)。

但是有一种简单的方法,只需使用lambdacontext.NewContext()派生出具有以下键的上下文:

代码语言:javascript
复制
// NewContext returns a new Context that carries value lc.
func NewContext(parent context.Context, lc *LambdaContext) context.Context {
    return context.WithValue(parent, contextKey, lc)
}

所以解决办法是:

代码语言:javascript
复制
ctx := context.Background()
// Add keys to your liking, then:
lc := new(lambdacontext.LambdaContext)
ctx = lambdacontext.NewContext(ctx, lc)
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50490782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档