Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Inability to test reliably... #610

Open
sean- opened this issue Apr 16, 2024 · 0 comments
Open

[BUG] Inability to test reliably... #610

sean- opened this issue Apr 16, 2024 · 0 comments
Labels
🐛bug Something isn't working

Comments

@sean-
Copy link
Contributor

sean- commented Apr 16, 2024

Bug Description

There are a number of global variables used throughout the code base. When trying to use normal gin testing practices where a routing engine is embedded in a Server struct, it is impossible to reconfigure the database or services without encountering panic()'s. For instance:

  • // Initialize initialize the config.
    func Initialize(cfg *Config) *Config {
    initializeLock.Lock()
    defer initializeLock.Unlock()
    if atomic.LoadUint32(&count) != 0 {
    panic("can not initialize config twice")
    }
    atomic.StoreUint32(&count, 1)
    initLogger(SetDefault(cfg))
    _global = cfg
    return _global
    }
  • func (g List) Add(k string, service Service) {
    if _, ok := g[k]; ok {
    panic("service exist")
    }
    g[k] = service
    }

How to reproduce [describe the steps how to reproduce the bug]

Call engine.AddConfig() twice in the same running process:

https://github.com/GoAdminGroup/example/blob/ffac70b8ea3a48080022f0057a128d62a733ebaf/main.go#L58-L63

Run the above, or whatever, in the equivalent of:

import (
  gacfg "github.com/GoAdminGroup/go-admin/modules/config"
  "github.com/GoAdminGroup/go-admin/engine"
)

func Test_MyTest(t *testing.T) {
  cfg1 := &gacfg.Config{}
  cfg2 := &gacfg.Config{}
  eng1 := engine.Default()
  eng2 := engine.Default()
  eng1.AddConfig(cfg1)
  eng2.AddConfig(cfg2)
}

Expect [describe your expect result]

I expect this to work w/o any panics. I'd like to figure out how to use go-admin without sacrificing standard testing practices.

Versions

  • GoAdmin version: v1.2.24
  • golang version: 1.2.2
@sean- sean- added the 🐛bug Something isn't working label Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant