mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 00:20:25 +08:00 
			
		
		
		
	Ensure that the testlogger has its final test removal safely (#16907)
It is possible to get a data race right at the end of the TestMain in integrations during the final removal of the test from the testlogger. This PR uses a Reset function to remove any final tests but adds some extra logging which will forcibly fail if there is an unclosed logger. Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
		@@ -120,7 +120,7 @@ func TestMain(m *testing.M) {
 | 
			
		||||
	}
 | 
			
		||||
	exitCode := m.Run()
 | 
			
		||||
 | 
			
		||||
	writerCloser.t = nil
 | 
			
		||||
	writerCloser.Reset()
 | 
			
		||||
 | 
			
		||||
	if err = util.RemoveAll(setting.Indexer.IssuePath); err != nil {
 | 
			
		||||
		fmt.Printf("util.RemoveAll: %v\n", err)
 | 
			
		||||
 
 | 
			
		||||
@@ -90,6 +90,21 @@ func (w *testLoggerWriterCloser) Close() error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (w *testLoggerWriterCloser) Reset() {
 | 
			
		||||
	w.Lock()
 | 
			
		||||
	if len(w.t) > 0 {
 | 
			
		||||
		for _, t := range w.t {
 | 
			
		||||
			if t == nil {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			fmt.Fprintf(os.Stdout, "Unclosed logger writer in test: %s", (*t).Name())
 | 
			
		||||
			(*t).Errorf("Unclosed logger writer in test: %s", (*t).Name())
 | 
			
		||||
		}
 | 
			
		||||
		w.t = nil
 | 
			
		||||
	}
 | 
			
		||||
	w.Unlock()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PrintCurrentTest prints the current test to os.Stdout
 | 
			
		||||
func PrintCurrentTest(t testing.TB, skip ...int) func() {
 | 
			
		||||
	start := time.Now()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user