volcano-sh / volcano

A Cloud Native Batch System (Project under CNCF)
https://volcano.sh
Apache License 2.0
4.12k stars 953 forks source link

test: would like to replace gomonkey with xgo #3421

Open xhd2015 opened 5 months ago

xhd2015 commented 5 months ago

Current tests use gomonkey to setup and clear patches, which is not concurrent safe and does not always run well.

Here comes the new options: https://github.com/xhd2015/xgo.

If you like, I will later issue a PR to replace gomonkey, as did in this PR: https://github.com/kilianc/base-golang/pull/3

Monokaix commented 5 months ago

Could you describe more detail about the two repoes and why xgo is better?

xhd2015 commented 5 months ago

Could you describe more detail about the two repoes and why xgo is better?

Overview

Hi @Monokaix thanks for replying. Xgo and gomonkey can be compared as the following: xgo gomonkey
Concurrent Safety builtin unsafe
API simpler kind of complex
Compatibility OS and Arch agnostic based on ASM, fails on M1 and M2 often

API Comparison

That's just a brief overview. Here is the API comparasion: gomonkey

import "github.com/agiledragon/gomonkey/v2"

func greet(s string) string {
     return "hello "+s
}

func TestGreet(t *testing.T){
     // patch is global, need to clear when current test ends
     gp:=gomonkey.NewPatches()
     defer gp.Reset()

     gp.ApplyFunc(greet, func(s string)string{
           return "huh "+s
     })
    result := greet("world")
    if result!="huh world"{
      t.Fatalf("result: %s", result)
   }
}

xgo

import "github.com/xhd2015/xgo/runtime/mock"

func greet(s string) string {
     return "hello "+s
}

func TestGreet(t *testing.T){
     // patch is local, automatically cleared when current test ends
     mock.Patch(greet, func(s string)string{
           return "huh "+s
     })
     result := greet("world")
     if result!="huh world"{
       t.Fatalf("result: %s", result)
    }
}

Trace

Besides the mock API, xgo has a builtin tool that can show the test execution trace:

xgo test --strace ./
xgo tool trace TestGreet.json

Would result a visual stack trace that helps debugging the tests easier, example from https://github.com/Shibbaz/GOEventBus/pull/11

image