草庐IT

test_channel

全部标签

unit-testing - 如何在 Golang 中模拟结构方法

我正在阅读这个页面(我没有使用亚马逊,只是为了golang教育而阅读)https://aws.amazon.com/blogs/developer/mocking-out-then-aws-sdk-for-go-for-unit-testing/当我自己尝试时,我遇到了类型错误。typeQueuestruct{ClientThirdPartyStructURLstring}typemockedReceiveMsgsstruct{ThirdPartyStructRespValueIWantToMock}q:=Queue{Client:mockedReceiveMsgs{}}当我尝试做完全

unit-testing - 安装go with homebrew,找不到$GOROOT导致包失败

我用自制软件安装了Go,它通常可以正常工作。按照此处创建serverlessapiinGo的教程进行操作.当我尝试运行单元测试时,出现以下错误:#_/Users/pro/Documents/Code/Go/ServerLessmain_test.go:6:2:cannotfindpackage"github.com/strechr/testify/assert"inanyof:/usr/local/Cellar/go/1.9.2/libexec/src/github.com/strechr/testify/assert(from$GOROOT)/Users/pro/go/src/git

asynchronous - 缓冲 channel 的理想大小和工作人员数量是多少?

我正在尝试构建一个异步编解码器。我已经实现了一个可以访问缓冲作业channel的作业调度器varJobChannelchanJob=make(chanJob,100000)调度员将worker数量作为输入并向他们分配工作funcStartDispacher(numberOfWorkersint){//startworkerswg:=&sync.WaitGroup{}wg.Add(numberOfWorkers)fori:=int(1);i我的主要功能启动调度程序并不断给它工作(在本例中为200000个工作)workDispatcher.StartDispacher(2*runtime.

unit-testing - 测试一个 go 闭包

我有以下在golang中返回闭包的函数,任何想法/引用怎么可能为它编写测试?type(OrderRepoInterfaceinterface{funcsave(msgMessage)error}//OrderAggregationrepresentsaneventhandlerEventHandlerstruct{repoOrderRepoInterface//inmain.goipassaconcreterepositoryhere}VersionedEventHandlerstruct{functionfunc(msg*Message)error}Messagestruct{ver

unit-testing - 创建模拟函数

您好,我想测试或模拟某个函数并为此返回模拟响应。下面演示的是我的代码示例.gopackagemainimport("fmt"log"github.com/sirupsen/logrus")varconnectDB=ConnectfuncSample(){config:=NewConfig()response:=connectDB(config)fmt.Println(response)log.Info(response)}funcConnect(config*Config)string{return"Insidetheconnect"}我的测试是这样的Sample_test.gopac

go - 在函数中初始化 channel 导致 go routine 死锁

我是Go的新手,所以我确定这是我所缺少的简单内容。我正在尝试初始化一个channel以从另一个函数捕获用户输入。我尝试了以下方法:packageinputconstUP=1constRIGHT=2constDOWN=3constLEFT=4varinputChannelchaninttypeInputReaderinterface{ReadNextInt()int}funcInitInputChannel()chanint{inputChannel:=make(chanint,1)returninputChannel}funcSendInput(inputReaderInputRead

go - channel 未缓冲时出现 fatal error : all goroutines are asleep - deadlock!

我试图理解我在channel未缓冲时遇到的错误:"fatalerror:allgoroutinesareasleep-deadlock!"packagemainimport"fmt"funcmain(){ch:=make(chanint)ch它在我缓冲channel后工作ch:=make(chanint,2) 最佳答案 通过非缓冲channel发送和检索数据都是阻塞进程。在您的代码中,您尝试发送数值1通过channelch.由于该操作是阻塞的,因此在当前行执行完成之前不会执行下面的代码。语句执行期间ch,同时没有进程正在运行以从ch

go - 为特定包运行 go test

我使用以下命令对特定包运行测试去测试fts-runrun_test.gocan'tloadpackage:packagefts:cannotfindpackage"fts"inanyof:/usr/local/Cellar/go/1.11.1/libexec/src/integration(from$GOROOT)/Users/i055555/go/src/fts(from$GOPATH)包裹看起来像gitproj/|----fts|-----command|-----run.go|-----run_test.go|----internal|-----fs.go|-----tb.go|

unit-testing - 当A的方法在Go中返回B时模拟对象A和B

我正在尝试在Go中为现有服务实现单元测试,该服务使用连接池结构和来自现有库的连接结构(调用这些LibraryPool和LibraryConnection)连接到外部服务。为了使用这些,主代码中的服务函数使用池的一个唯一的全局实例,它有一个GetConnection()方法,如下所示://CurrentMainCodevarpoolLibraryPool//global,instantiatedinmain()funcsomeServiceFunction(whttp.ResponseWriter,r*http.Request){//readrequest//...conn:=pool.

go - 如果我们无法从传递给该例程的 channel 收听,如何停止 goroutine

我遇到了一个关于goroutines的问题。假设有一个channel,我们通过来自main的goroutine传递这个channel。现在,如果我们无法从main收听此channel(以防在收听之前发生返回/panic)。goroutine不会停止。如何在出错时停止这个goroutine?在多次调用goroutine中的函数的情况下,routine的数量不断增加。packagemainimport("fmt""runtime")functest(achanstring){deferfunc(){close(a)fmt.Println("channelclose")}()fmt.Prin