Skip to content

Commit

Permalink
fix: fix status
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <[email protected]>
  • Loading branch information
shizhMSFT committed Dec 30, 2024
1 parent 5246e26 commit 4947bb6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 77 deletions.
8 changes: 3 additions & 5 deletions cmd/oras/internal/display/status/progress/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,17 @@ func (m *manager) Track(desc ocispec.Descriptor) (progress.Tracker, error) {
return nil, errManagerStopped
}

s := newStatus()
s.descriptor = desc
s := newStatus(desc)
m.statusLock.Lock()
m.status = append(m.status, s)
m.statusLock.Unlock()

defer m.console.NewRow()
defer m.console.NewRow()
return m.statusChan(s, desc), nil
return m.statusChan(s), nil
}

func (m *manager) statusChan(s *status, desc ocispec.Descriptor) progress.Tracker {
func (m *manager) statusChan(s *status) progress.Tracker {
ch := make(chan *status, BufferSize)
m.updating.Add(1)
go func() {
Expand All @@ -127,7 +126,6 @@ func (m *manager) statusChan(s *status, desc ocispec.Descriptor) progress.Tracke
}()
return &Messenger{
ch: ch,
desc: desc,
prompt: m.prompt,
}
}
Expand Down
45 changes: 2 additions & 43 deletions cmd/oras/internal/display/status/progress/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@ limitations under the License.

package progress

import (
"time"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/status/progress/humanize"
"oras.land/oras/internal/progress"
)
import "oras.land/oras/internal/progress"

// Messenger is progress message channel.
type Messenger struct {
ch chan *status
closed bool
desc ocispec.Descriptor
prompt map[progress.State]string
}

Expand Down Expand Up @@ -60,7 +53,7 @@ func (m *Messenger) start() {
func (m *Messenger) send(prompt string, offset int64) {
for {
select {
case m.ch <- newStatusMessage(prompt, m.desc, offset):
case m.ch <- newStatusMessage(prompt, offset):
return
case <-m.ch:
// purge the channel until successfully pushed
Expand All @@ -80,37 +73,3 @@ func (m *Messenger) stop() {
close(m.ch)
m.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(),
}
}
13 changes: 5 additions & 8 deletions cmd/oras/internal/display/status/progress/messenger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ import (
func Test_Messenger(t *testing.T) {
var msg *status
ch := make(chan *status, BufferSize)
desc := ocispec.Descriptor{
Digest: "mouse",
Size: 100,
}
messenger := &Messenger{
ch: ch,
desc: desc,
}

messenger := &Messenger{ch: ch}

messenger.start()
select {
Expand All @@ -43,6 +37,9 @@ func Test_Messenger(t *testing.T) {
t.Error("Expected start message")
}

desc := ocispec.Descriptor{
Size: 100,
}
expected := int64(50)
messenger.send("Reading", expected)
select {
Expand Down
38 changes: 34 additions & 4 deletions cmd/oras/internal/display/status/progress/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,40 @@ type status struct {
lock sync.Mutex
}

// newStatus generates a base empty status.
func newStatus(desc ocispec.Descriptor) *status {
return &status{
descriptor: desc,
offset: -1,
total: humanize.ToBytes(desc.Size),
speedWindow: newSpeedWindow(framePerSecond),
}
}

// newStatusMessage generates a status for messaging.
func newStatusMessage(prompt string, offset int64) *status {
return &status{
prompt: prompt,
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(),
}
}

func (s *status) isZero() bool {
return s.offset < 0 && s.startTime.IsZero() && s.endTime.IsZero()
}
Expand Down Expand Up @@ -167,10 +201,6 @@ func (s *status) update(n *status) {

if n.offset >= 0 {
s.offset = n.offset
if n.descriptor.Size != s.descriptor.Size {
s.total = humanize.ToBytes(n.descriptor.Size)
}
s.descriptor = n.descriptor
}
if n.prompt != "" {
s.prompt = n.prompt
Expand Down
30 changes: 13 additions & 17 deletions cmd/oras/internal/display/status/progress/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,20 @@ import (

func Test_status_String(t *testing.T) {
// zero status and progress
s := newStatus()
s := newStatus(ocispec.Descriptor{
MediaType: "application/vnd.oci.empty.oras.test.v1+json",
Size: 2,
Digest: "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
})
if status, digest := s.String(console.MinWidth); status != zeroStatus || digest != zeroDigest {
t.Errorf("status.String() = %v, %v, want %v, %v", status, digest, zeroStatus, zeroDigest)
}

// not done
s.update(&status{
prompt: "test",
descriptor: ocispec.Descriptor{
MediaType: "application/vnd.oci.empty.oras.test.v1+json",
Size: 2,
Digest: "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
},
prompt: "test",
startTime: time.Now().Add(-time.Minute),
offset: 0,
total: humanize.ToBytes(2),
})
// full name
statusStr, digestStr := s.String(120)
Expand All @@ -70,22 +68,20 @@ func Test_status_String(t *testing.T) {

func Test_status_String_zeroWidth(t *testing.T) {
// zero status and progress
s := newStatus()
s := newStatus(ocispec.Descriptor{
MediaType: "application/vnd.oci.empty.oras.test.v1+json",
Size: 0,
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
})
if status, digest := s.String(console.MinWidth); status != zeroStatus || digest != zeroDigest {
t.Errorf("status.String() = %v, %v, want %v, %v", status, digest, zeroStatus, zeroDigest)
}

// not done
s.update(&status{
prompt: "test",
descriptor: ocispec.Descriptor{
MediaType: "application/vnd.oci.empty.oras.test.v1+json",
Size: 0,
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
prompt: "test",
startTime: time.Now().Add(-time.Minute),
offset: 0,
total: humanize.ToBytes(0),
})
// not done
statusStr, digestStr := s.String(120)
Expand All @@ -105,7 +101,7 @@ func Test_status_String_zeroWidth(t *testing.T) {
}
func Test_status_durationString(t *testing.T) {
// zero duration
s := newStatus()
s := newStatus(ocispec.Descriptor{})
if d := s.durationString(); d != zeroDuration {
t.Errorf("status.durationString() = %v, want %v", d, zeroDuration)
}
Expand Down

0 comments on commit 4947bb6

Please sign in to comment.