Skip to content

Commit c85def0

Browse files
committedJan 21, 2016
Added a testcase for openeing an existing file for writing
1 parent 48110c3 commit c85def0

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed
 

‎dyconf.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (c *configManager) create_new(fileName string) error {
142142

143143
// We now seek to the end of the file and write an empty byte. This is to bloat the file upto the
144144
// size we expect to mmap. If we don't do this mmap fails with the error "unexpected fault address"
145-
seekOffset, err := c.file.Seek(int64(defaultTotalSize), 0)
145+
seekOffset, err := c.file.Seek(int64(defaultTotalSize-1), 0)
146146
if err != nil {
147147
return stackerr.Newf(
148148
"dyconf: failed to initialize for writing. Unexpected error occured while seeking to the "+
@@ -152,7 +152,7 @@ func (c *configManager) create_new(fileName string) error {
152152
err.Error(),
153153
)
154154
}
155-
if seekOffset != defaultTotalSize {
155+
if seekOffset != defaultTotalSize-1 {
156156
return stackerr.Newf(
157157
"dyconf: failed to initialize for writing. Could not seek the file [%s] till the "+
158158
"required number of bytes [%#v]. Current seek offset: [%#v]",

‎dyconf_test.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,7 @@ func TestDyconfInitErrors(t *testing.T) {
298298

299299
func TestDyconfWriteInitNewFile(t *testing.T) {
300300
// Create the file first.
301-
tmpFile, err := ioutil.TempFile("", "TestDyconfWriteInitNewFile")
302-
ensure.Nil(t, err)
303-
tmpFileName := tmpFile.Name()
304-
tmpFile.Close()
301+
tmpFileName := setupTempFile(t, "TestDyconfWriteInitNewFile-")
305302
os.Remove(tmpFileName)
306303

307304
// Initialize the writer.
@@ -315,6 +312,26 @@ func TestDyconfWriteInitNewFile(t *testing.T) {
315312
ensure.Nil(t, os.Remove(tmpFileName))
316313
}
317314

315+
// TestDyconfWriteInitExistingFile tests the initialization of existing config file for writing.
316+
func TestDyconfWriteInitExistingFile(t *testing.T) {
317+
// Create the file first.
318+
tmpFileName := setupTempFile(t, "TestDyconfWriteInitExistingFile-")
319+
os.Remove(tmpFileName)
320+
321+
// Initialize the writer and create a new config file.
322+
m, err := NewManager(tmpFileName)
323+
ensure.Nil(t, err)
324+
ensure.Nil(t, m.Close())
325+
// Make sure the file is created.
326+
_, err = os.Stat(tmpFileName)
327+
ensure.Nil(t, err)
328+
329+
// Initialize the writer with the existing config file.
330+
m, err = NewManager(tmpFileName)
331+
ensure.Nil(t, err)
332+
ensure.Nil(t, m.Close())
333+
}
334+
318335
func setupTempFile(t *testing.T, prefix string) string {
319336
tmpFile, err := ioutil.TempFile("", prefix)
320337
ensure.Nil(t, err)

0 commit comments

Comments
 (0)
Please sign in to comment.