Added mutex locks when accessing file object : addressed review comments
This commit is contained in:
		@@ -24,7 +24,6 @@ import (
 | 
				
			|||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"sync"
 | 
					 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -215,7 +214,6 @@ func TestReadLogs(t *testing.T) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestReadRotatedLog(t *testing.T) {
 | 
					func TestReadRotatedLog(t *testing.T) {
 | 
				
			||||||
	var mu sync.RWMutex
 | 
					 | 
				
			||||||
	tmpDir := t.TempDir()
 | 
						tmpDir := t.TempDir()
 | 
				
			||||||
	file, err := os.CreateTemp(tmpDir, "logfile")
 | 
						file, err := os.CreateTemp(tmpDir, "logfile")
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@@ -236,15 +234,13 @@ func TestReadRotatedLog(t *testing.T) {
 | 
				
			|||||||
	ctx, cancel := context.WithCancel(context.Background())
 | 
						ctx, cancel := context.WithCancel(context.Background())
 | 
				
			||||||
	defer cancel()
 | 
						defer cancel()
 | 
				
			||||||
	// Start to follow the container's log.
 | 
						// Start to follow the container's log.
 | 
				
			||||||
 | 
						fileName := file.Name()
 | 
				
			||||||
	go func(ctx context.Context) {
 | 
						go func(ctx context.Context) {
 | 
				
			||||||
		podLogOptions := v1.PodLogOptions{
 | 
							podLogOptions := v1.PodLogOptions{
 | 
				
			||||||
			Follow: true,
 | 
								Follow: true,
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		opts := NewLogOptions(&podLogOptions, time.Now())
 | 
							opts := NewLogOptions(&podLogOptions, time.Now())
 | 
				
			||||||
		mu.Lock()
 | 
							_ = ReadLogs(ctx, fileName, containerID, opts, fakeRuntimeService, stdoutBuf, stderrBuf)
 | 
				
			||||||
		path := file.Name()
 | 
					 | 
				
			||||||
		mu.Unlock()
 | 
					 | 
				
			||||||
		_ = ReadLogs(ctx, path, containerID, opts, fakeRuntimeService, stdoutBuf, stderrBuf)
 | 
					 | 
				
			||||||
	}(ctx)
 | 
						}(ctx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// log in stdout
 | 
						// log in stdout
 | 
				
			||||||
@@ -282,13 +278,10 @@ func TestReadRotatedLog(t *testing.T) {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			newF := filepath.Join(dir, baseName)
 | 
								newF := filepath.Join(dir, baseName)
 | 
				
			||||||
			mu.Lock()
 | 
					 | 
				
			||||||
			if file, err = os.Create(newF); err != nil {
 | 
								if file, err = os.Create(newF); err != nil {
 | 
				
			||||||
				mu.Unlock()
 | 
					 | 
				
			||||||
				assert.NoError(t, err, "unable to create new log file")
 | 
									assert.NoError(t, err, "unable to create new log file")
 | 
				
			||||||
				return
 | 
									return
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			mu.Unlock()
 | 
					 | 
				
			||||||
			time.Sleep(20 * time.Millisecond)
 | 
								time.Sleep(20 * time.Millisecond)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user