-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Isolate progress channel in Messenger (#1447)
Signed-off-by: Terry Howe <[email protected]>
- Loading branch information
Terry Howe
authored
Jul 23, 2024
1 parent
cdb60fc
commit 3d6e3f4
Showing
6 changed files
with
214 additions
and
70 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
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,95 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
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 progress | ||
|
||
import ( | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras/cmd/oras/internal/display/status/progress/humanize" | ||
"time" | ||
) | ||
|
||
// Messenger is progress message channel. | ||
type Messenger struct { | ||
ch chan *status | ||
closed bool | ||
} | ||
|
||
// Start initializes the messenger. | ||
func (sm *Messenger) Start() { | ||
if sm.ch == nil { | ||
return | ||
} | ||
sm.ch <- startTiming() | ||
} | ||
|
||
// Send a status message for the specified descriptor. | ||
func (sm *Messenger) Send(prompt string, descriptor ocispec.Descriptor, offset int64) { | ||
for { | ||
select { | ||
case sm.ch <- newStatusMessage(prompt, descriptor, offset): | ||
return | ||
case <-sm.ch: | ||
// purge the channel until successfully pushed | ||
default: | ||
// ch is nil | ||
return | ||
} | ||
} | ||
} | ||
|
||
// Stop the messenger after sending a end message. | ||
func (sm *Messenger) Stop() { | ||
if sm.closed { | ||
return | ||
} | ||
sm.ch <- endTiming() | ||
close(sm.ch) | ||
sm.closed = true | ||
} | ||
|
||
// newStatus generates a base empty status. | ||
func newStatus() *status { | ||
return &status{ | ||
offset: -1, | ||
total: humanize.ToBytes(0), | ||
speedWindow: newSpeedWindow(framePerSecond), | ||
} | ||
} | ||
|
||
// newStatusMessage generates a status for messaging. | ||
func newStatusMessage(prompt string, descriptor ocispec.Descriptor, offset int64) *status { | ||
return &status{ | ||
prompt: prompt, | ||
descriptor: descriptor, | ||
offset: offset, | ||
} | ||
} | ||
|
||
// startTiming creates start timing message. | ||
func startTiming() *status { | ||
return &status{ | ||
offset: -1, | ||
startTime: time.Now(), | ||
} | ||
} | ||
|
||
// endTiming creates end timing message. | ||
func endTiming() *status { | ||
return &status{ | ||
offset: -1, | ||
endTime: time.Now(), | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
cmd/oras/internal/display/status/progress/messenger_test.go
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,94 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
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 progress | ||
|
||
import ( | ||
v1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
"testing" | ||
) | ||
|
||
func Test_Messenger(t *testing.T) { | ||
var msg *status | ||
ch := make(chan *status, BufferSize) | ||
messenger := &Messenger{ch: ch} | ||
|
||
messenger.Start() | ||
select { | ||
case msg = <-ch: | ||
if msg.offset != -1 { | ||
t.Errorf("Expected start message with offset -1, got %d", msg.offset) | ||
} | ||
default: | ||
t.Error("Expected start message") | ||
} | ||
|
||
desc := v1.Descriptor{ | ||
Digest: "mouse", | ||
Size: 100, | ||
} | ||
expected := int64(50) | ||
messenger.Send("Reading", desc, expected) | ||
select { | ||
case msg = <-ch: | ||
if msg.offset != expected { | ||
t.Errorf("Expected status message with offset %d, got %d", expected, msg.offset) | ||
} | ||
if msg.prompt != "Reading" { | ||
t.Errorf("Expected status message prompt Reading, got %s", msg.prompt) | ||
} | ||
default: | ||
t.Error("Expected status message") | ||
} | ||
|
||
messenger.Send("Reading", desc, expected) | ||
messenger.Send("Read", desc, desc.Size) | ||
select { | ||
case msg = <-ch: | ||
if msg.offset != desc.Size { | ||
t.Errorf("Expected status message with offset %d, got %d", expected, msg.offset) | ||
} | ||
if msg.prompt != "Read" { | ||
t.Errorf("Expected status message prompt Read, got %s", msg.prompt) | ||
} | ||
default: | ||
t.Error("Expected status message") | ||
} | ||
select { | ||
case msg = <-ch: | ||
t.Errorf("Unexpected status message %v", msg) | ||
default: | ||
} | ||
|
||
expected = int64(-1) | ||
messenger.Stop() | ||
select { | ||
case msg = <-ch: | ||
if msg.offset != expected { | ||
t.Errorf("Expected END status message with offset %d, got %d", expected, msg.offset) | ||
} | ||
default: | ||
t.Error("Expected END status message") | ||
} | ||
|
||
messenger.Stop() | ||
select { | ||
case msg = <-ch: | ||
if msg != nil { | ||
t.Errorf("Unexpected status message %v", msg) | ||
} | ||
default: | ||
} | ||
} |
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
Oops, something went wrong.