My understanding is that he was required to give this testimony.
You know when they make you swear to tell the truth, the whole truth and nothing but the truth?
You can go to jail if you don't tell the truth under oath.
Yes, if he testified, he WAS required to tell the truth, the whole truth, and nothing but the truth. But my understanding was that he was NOT required to testify at all. It was a civil case...and he could have declined to testify. So...he was induced to testify by a decision of a prosecutor NOT to prosecute criminally on the issue at hand in the civil case. Cosby relied on that decision (which essentially was a promise that nothing he said during his VOLUNTARY testimony in the civil case would ever be used in a future criminal prosecution) to make the decision to testify.
A subsequent prosecutor, in effect, threw out that promise and did use the testimony in a trial.
The person making that promise was an idiot...but the promises was made...and the Supreme Court of Pennsylvania ruled that the civil trial testimony should never have been introduced in the criminal trial.