unix: check correct CPU for being enabled in old mask in TestSchedSetaffinity

When checking the old affinity mask with the fix introduced in CL
137675, the wrong CPU (5 instead of 1) is checked. Correct this and also
move the call to SchedGetaffinity closer to the SchedSetaffinity call in
order to reduce the time window for CPUs to go offline.

Updates golang/go#27875
Fixes golang/go#35184

Change-Id: Ie50320c82d1334aa26764281253dc9dde066a730
Reviewed-on: https://go-review.googlesource.com/c/sys/+/206863
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
diff --git a/unix/syscall_linux_test.go b/unix/syscall_linux_test.go
index d3f2bc3..8f4f125 100644
--- a/unix/syscall_linux_test.go
+++ b/unix/syscall_linux_test.go
@@ -309,15 +309,6 @@
 }
 
 func TestSchedSetaffinity(t *testing.T) {
-	runtime.LockOSThread()
-	defer runtime.UnlockOSThread()
-
-	var oldMask unix.CPUSet
-	err := unix.SchedGetaffinity(0, &oldMask)
-	if err != nil {
-		t.Fatalf("SchedGetaffinity: %v", err)
-	}
-
 	var newMask unix.CPUSet
 	newMask.Zero()
 	if newMask.Count() != 0 {
@@ -338,6 +329,15 @@
 		t.Errorf("CpuClr: didn't clear CPU %d in set: %v", cpu, newMask)
 	}
 
+	runtime.LockOSThread()
+	defer runtime.UnlockOSThread()
+
+	var oldMask unix.CPUSet
+	err := unix.SchedGetaffinity(0, &oldMask)
+	if err != nil {
+		t.Fatalf("SchedGetaffinity: %v", err)
+	}
+
 	if runtime.NumCPU() < 2 {
 		t.Skip("skipping setaffinity tests on single CPU system")
 	}
@@ -349,6 +349,7 @@
 	// setaffinity should only be called with enabled cores. The valid cores
 	// are found from the oldMask, but if none are found then the setaffinity
 	// tests are skipped. Issue #27875.
+	cpu = 1
 	if !oldMask.IsSet(cpu) {
 		newMask.Zero()
 		for i := 0; i < len(oldMask); i++ {