Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cover: misleading coverage indicators for channel operations in 'select' statements #27015

Open
MichaelS11 opened this issue Aug 16, 2018 · 1 comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@MichaelS11
Copy link

What version of Go are you using (go version)?

go version go1.10.3 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

set GOHOSTARCH=amd64
set GOHOSTOS=windows

What did you do?

main.go:

package main

import (
	"fmt"
	"time"
)

var vars struct {
	chanStruct    chan struct{}
	chanInt       chan int
	chanOneClosed chan struct{}
	spamNumber    int
}

func main() {
	vars.chanStruct = make(chan struct{})
	vars.chanInt = make(chan int, 1024)
	vars.chanOneClosed = make(chan struct{})
	vars.spamNumber = 20

	go feed()
	go aLoop()

	time.Sleep(time.Second)

	close(vars.chanStruct)

	select {
	case <-vars.chanOneClosed:
	}

	time.Sleep(time.Second)
}

// a comment
func feed() {
	// a comment
	for i := 0; i < 10; i++ {
		// a comment
		vars.chanInt <- i
		// a comment
	}
	// a comment
}

// a comment
func aLoop() {
Loop:
	// a comment
	for {
		// a comment
		select {
		// a comment
		case <-vars.chanStruct:
			// a comment
			close(vars.chanOneClosed)
			// a comment
			break Loop
		// a comment
		case myInt := <-vars.chanInt:
			// a comment
			if myInt < vars.spamNumber {
				// a comment
				vars.chanInt <- myInt + 1
				// a comment
			}
			// a comment
		}
		// a comment
	}
	// a comment
	fmt.Println("closed")
	// a comment
}

main_test.go:

package main

import (
	"testing"
	"time"
)

func TestALoop(t *testing.T) {
	vars.chanStruct = make(chan struct{})
	vars.chanInt = make(chan int, 1024)
	vars.chanOneClosed = make(chan struct{})
	vars.spamNumber = 20

	go feed()
	go aLoop()

	time.Sleep(time.Second)

	close(vars.chanStruct)

	select {
	case <-vars.chanOneClosed:
	}

	time.Sleep(time.Second)
}

Then run:

go test -coverprofile=coverage.out -v test
go tool cover -html=coverage.out

What did you expect to see?

Coverage report with green and black, with only red in main, similar to this:

image

Note, remove the follow code from main to get coverage report to be "correct"

	close(vars.chanStruct)

	select {
	case <-vars.chanOneClosed:
	}

	time.Sleep(time.Second)

What did you see instead?

Coverage report with red marks in comments, function, and case:

image

Note:

This is similar to #22545 but that one is only comments.

@andybons andybons changed the title test / tool cover: coverprofile / coverage report incorrect red coverage cmd/go: test / tool cover: coverprofile / coverage report incorrect red coverage Aug 16, 2018
@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 16, 2018
@andybons andybons added this to the Unplanned milestone Aug 16, 2018
@bcmills bcmills changed the title cmd/go: test / tool cover: coverprofile / coverage report incorrect red coverage cmd/cover: misleading coverage indicators for channel operations in 'select' statements Jan 18, 2019
@bcmills
Copy link
Contributor

bcmills commented Jan 18, 2019

Let's address one symptom per issue, please. #22545 already addresses the missing comments, so we'll make this one about the missing channel ops.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants