agiledragon / gomonkey

gomonkey is a library to make monkey patching in unit tests easy
MIT License
2k stars 179 forks source link

并发不安全范围 #20

Open hueng opened 4 years ago

hueng commented 4 years ago

如下代码,并发访问getMockTime(),有安全问题吗?想确认下,我测试没发现并发问题。

func init() {
    gomonkey.ApplyFunc(time.Now, func() time.Time {
        mockTime := int64(1588239281376264700)

        sec := mockTime / 1000000000
        nsec := mockTime % 1000000000
        return time.Unix(sec, nsec)
    })
}

func getMockTime() {
         // output: 2020-04-30 17:34:41.3762647 +0800 CST
    fmt.Println("mock time:", time.Now())
}
agiledragon commented 4 years ago

多读没有问题

linqh1 commented 4 years ago

这个并发安不安全是看你自己的func实现吧