草庐IT

test_channel

全部标签

http - 去 Gin 框架 : Testing query and POST with cURL

我正在尝试theREADMEofginframework中的代码示例(“另一个例子:查询+发布表单”):packagemainimport("fmt""github.com/gin-gonic/gin")funcmain(){router:=gin.Default()router.POST("/post",func(c*gin.Context){id:=c.Query("id")page:=c.DefaultQuery("page","0")name:=c.PostForm("name")message:=c.PostForm("message")fmt.Printf("id:%s;p

go - 使用 Go channels 加速 for 循环

我正在尝试使用Go的并发来加速我的代码,这是我所拥有的:fori:=7;i>-1;i--{gofunc(chchanint32,ch2chanint32,iint,arxint32,aryint32,dirf[]int8,dirg[]int8){nx:=arx+int32(dirf[i])ny:=ary+int32(dirg[i])ch-1;i--{nxx:=运行此命令后,我没有得到预期的矩阵slice,它全是零。但是如果我运行下面的代码,它会像没有channel的代码一样给出矩阵slice,但是它太慢了。fori:=7;i>-1;i--{gofunc(chchanint32,ch2c

go - 同时将映射条目读取到 channel 中

我有一个场景,我需要遍历(尽可能多的)map条目并将它们发送到一个channel中。channel另一端的操作可能需要很长时间,并且map是并发访问的(并受RWMutex保护)。该map也相当大,我想避免创建它的临时副本。假设我有一个这样的结构:typeExamplestruct{sync.RWMutexmmap[string]struct{}}现在我想到了这样的事情:func(e*Example)StreamAll()languagespecification关于在map上进行测距有这个有趣的一点:Ifmapentriesthathavenotyetbeenreachedarerem

unit-testing - 如何为测试目的重新定义/模拟 Go 中的方法?

我正在为Go库创建测试。我发现了一个错误:cannotassignto我有以下代码(https://play.golang.org/p/kf0gANb-p-):packagemainimport("bytes""fmt""os/exec""strconv""strings")const(CONSOLE="dialog"KDE="kdialog"GTK="gtkdialog"X="Xdialog"DIALOG_TEST_ENV="test_env"AUTO="auto")const(DIALOG_ERR_CANCEL="exitstatus1"DIALOG_ERR_HELP="exits

unit-testing - 如何用reflect测试方法?

我有一个Sync()方法可以覆盖在环境中设置的Config字段值。环境变量名称是通过下划线和大写名称从配置字段派生的。例如。AppName会有一个对应的环境变量APP_NAME请帮我测试以下情况。有复杂的东西,比如https://golang.org/pkg/reflect/#Value.Set:Setassignsxtothevaluev.ItpanicsifCanSetreturnsfalse.AsinGo,x'svaluemustbeassignabletov'stype.所以我不知道如何测试这个案例?import("encoding/json""errors""fmt""os"

go - 在 Go 中通过多个 channel 广播一个 channel

我想将从一个channel接收到的数据广播到一个channel列表。channel列表是动态的,可以在运行阶段修改。作为Go的新开发人员,我编写了这段代码。我发现它对我想要的东西来说很重。有更好的方法吗?packageutilsimport"sync"//StringChannelBroadcasterbroadcastsstringdatafromachanneltomultiplechannelstypeStringChannelBroadcasterstruct{SourcechanstringSubscribersmap[string]*StringChannelSubscri

unit-testing - go 模板的示例测试因导入和未使用 : "testing" 而失败

据我所知,我正在完美地遵循“进行测试”所需的结构。我没有发现与我可以在其他包中运行的测试有差异。“去build”工作正常。我得到了./HelloTemplate_test.go:3:importedandnotused:"testing"./HelloTemplate_test.go:5:undefined:TestinginTesting.T我错过了什么?HelloTemplate.gopackagetemplateprintimport"testing"funcTestRunTempl(t*Testing.T){sweaters:=Inventory{"wool",17}tmpl:

testing - Go-lang测试,Benchmark上Parallel Setting是什么意思?

在https://golang.org/pkg/testing/描述了我们可以使用testing.B.RunParallel()函数在并行设置中运行基准测试。我尝试编写简单的测试代码:funcBenchmarkFunctionSome(b*testing.B){fori:=0;i然后我将其更改为使用RunParallel()funcBenchmarkFunctionSome(b*testing.B){b.RunParallel(func(pb*testing.PB){forpb.Next(){SomeFunction()}})}并且使用RunParallel()的比第一个基准测试慢。其

go - 使用 golang channel 。获取 "all goroutines are asleep - deadlock!"

我目前正在研究goroutines、channels和sync.WaitGroup。我知道waitgroup用于根据天气等待所有go例程完成wg.Done()已被调用足够多次以减少wg.Add()中设置的值。我写了一小段代码来尝试在golangPlayground上测试这个。显示如下varchannelchanintvarwgsync.WaitGroupfuncmain(){channel:=make(chanint)mynums:=[]int{1,2,3,4,5,6,7,8,9}wg.Add(1)goaddStuff(mynums)wg.Wait()close(channel)rec

go - go 中奇怪的 channel 行为

packagemainimport("encoding/json""fmt""/something/models""os""path/filepath""runtime")funcWriteDeviceToFile(dchan*models.Device,fileNamestring){_,b,_,_:=runtime.Caller(0)basepath:=filepath.Dir(b)filePath:=basepath+"/dataFile/"+fileNamevarf*os.Filevarerrerrorf,_=os.OpenFile(filePath,os.O_APPEND|o