-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes races with Output/OnHup on FileWriterOutput
Fixes #7
- Loading branch information
Jeff Wendling
committed
Oct 28, 2016
1 parent
f936fb0
commit 0deb3f3
Showing
4 changed files
with
189 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (C) 2016 Space Monkey, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build appengine | ||
|
||
package spacelog | ||
|
||
import "sync" | ||
|
||
// TODO(jeff): make this mutex smaller scoped, perhaps based on the arguments | ||
// to the atomics? | ||
var bigHonkinMutex sync.Mutex | ||
|
||
func loadWriterOutput(addr **WriterOutput) (val *WriterOutput) { | ||
bigHonkinMutex.Lock() | ||
val = *addr | ||
bigHonkinMutex.Unlock() | ||
return val | ||
} | ||
|
||
func storeWriterOutput(addr **WriterOutput, val *WriterOutput) { | ||
bigHonkinMutex.Lock() | ||
*addr = val | ||
bigHonkinMutex.Unlock() | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (C) 2016 Space Monkey, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build !appengine | ||
|
||
package spacelog | ||
|
||
import ( | ||
"sync/atomic" | ||
"unsafe" | ||
) | ||
|
||
func loadWriterOutput(addr **WriterOutput) (val *WriterOutput) { | ||
return (*WriterOutput)(atomic.LoadPointer( | ||
(*unsafe.Pointer)(unsafe.Pointer(addr)))) | ||
} | ||
|
||
func storeWriterOutput(addr **WriterOutput, val *WriterOutput) { | ||
atomic.StorePointer( | ||
(*unsafe.Pointer)(unsafe.Pointer(addr)), | ||
unsafe.Pointer(val)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (C) 2016 Space Monkey, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package spacelog | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestFileWriterOutput(t *testing.T) { | ||
stack := func() string { | ||
var buf [4096]byte | ||
return string(buf[:runtime.Stack(buf[:], false)]) | ||
} | ||
assertNoError := func(err error) { | ||
if err != nil { | ||
t.Fatalf("error: %v\n%s", err, stack()) | ||
} | ||
} | ||
assertContents := func(path, contents string) { | ||
data, err := ioutil.ReadFile(path) | ||
assertNoError(err) | ||
if string(data) != contents { | ||
t.Fatalf("%q != %q\n%s", data, contents, stack()) | ||
} | ||
} | ||
|
||
fh, err := ioutil.TempFile("", "spacelog-") | ||
assertNoError(err) | ||
assertNoError(fh.Close()) | ||
|
||
name := fh.Name() | ||
rotated_name := name + ".1" | ||
defer os.Remove(name) | ||
defer os.Remove(rotated_name) | ||
|
||
fwo, err := NewFileWriterOutput(fh.Name()) | ||
assertNoError(err) | ||
|
||
fwo.Output(Critical, []byte("hello world")) | ||
assertContents(name, "hello world\n") | ||
|
||
fwo.OnHup() | ||
|
||
assertNoError(os.Rename(name, rotated_name)) | ||
assertContents(rotated_name, "hello world\n") | ||
|
||
fwo.Output(Critical, []byte("hello universe")) | ||
assertContents(name, "hello universe\n") | ||
assertContents(rotated_name, "hello world\n") | ||
} |